跳到正文

个人记录

暑假-S组-第八场

Fire_Kylin2约 7 分钟
原文
文章信息与版本

文章信息

发布
2025年8月22日
收录
2025年12月12日
更新
2026年11月16日
篇幅
约 7 分钟

版本

  1. v1当前

概述

传送门

回到中位数

题目编号T1T2T3T4
题目难度黄黄黄绿
考察知识桶 ++ 双指针数学 ++ 贪心模拟 ++ 推导数学 ++ 二分

分析

T1

Wrong Cause

发现了需要使用双指针,但不知道既然要桶

Solution

Num.1 15pts\color{red}15pts

考虑暴力搜索,两个一组枚举,O(n3)O(n^3)

Num.2 50pts\color{orange}50pts

发现 ww 的数据范围较小,我们枚举 ww 的总和,分组自然是大的和小的搭配(因为得到的组数一定最多),所以排序之后就双指针即可

Num.3 100pts\color{green}100pts

双指针算法还是慢了。由之前发现了 ww 的范围小,所以我们可以拆分 ww ,比如 总和=100总和 = 100 的时候,枚举一个 w1w1 ,得到另外一个 w2=100−w1w2 = 100 - w1 ,直接用桶存储来二者的数量,取最小值,但是需要特殊注意二者相等的情况。此时的时间复杂度是:O(k∗T)O(k∗T) ,kk 的范围是[0,20000] [0,20000],炸不了一点

ACcode

C++
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 7;
int a[N], n, pail[N];
void solve()
{
	int mmax = 0;
	for (int w = 2; w <= 200; w++)
	{
		int cnt = 0;
		for (int i = 1; i <= 100; i++)
		{
			if (w - i >= 1)
			{
				int x = i, y = w - i;
				if (x > y) continue;
				if (x == y) cnt += pail[x] / 2;
				else cnt += min(pail[x], pail[y]);
			}
		}
		mmax = max(mmax, cnt);
	}
	cout << mmax << "\n";
}
int main()
{
	ios::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	int t;
	cin >> t;
	while (t--)
	{
		cin >> n;
		for (int i = 1; i<= n; i++)
		{
			cin >> a[i];
			pail[a[i]] ++;
		}
		solve();
		for (int i = 1; i <= 100; i++) pail[i] = 0;
	}
	return 0;
}

T2

Wrong Cause

思路算是找对了,但是忽略掉了递归出答案后的还是在递归浪费时间。

Solution

  • 想让最后的结果小,那么就有了下面两条有优先级的原则:
  1. 尽量位数小
  2. 尽量小数码在前
  • 于是从 99 开始往 22 考虑,只要是因数就除。除到出不了为止

ACcode

C++
#include <bits/stdc++.h>
#define int long long
using namespace std;
int solve(int x)
{
	if (x < 10) return x + 10;
	vector<int> ans;
	while (x > 9)
	{
		bool flag = 0;
		for (int i = 9; i >= 2; i--)
		{
			if (x % i == 0)
			{
				flag = 1;
				ans.push_back(i);
				x /= i;
				break;
			}
		}
		if (!flag) return -1;
	}
	ans.push_back(x);
	sort(ans.begin(), ans.end());
	int sum = 0;
	for (int i = 0; i < ans.size(); i++)
		sum = sum * 10 + ans[i];
	return sum;
}
signed main()
{
	ios::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	int t;
	cin >> t;
	while (t--)
	{
		int n;
		cin >> n;
		cout << solve(n) << "\n";
	}
	return 0;
}

T3

Wrong Cause

写了个假解,运气好拿了 90pts\color{green}90pts

Solution

  • 暴力模拟必然超时,观察发现每操作 44 次,得到的相对位移是固定的。那么先暴力求出一回 44 次,将相对位移重复计算(即乘)n4\frac{n}{4} 次,可能最后还剩了 n % 4 次操作,暴力即可

ACcode

C++
#include <bits/stdc++.h>
using namespace std;
#define int long long  
const int N = 5e5 + 10;
int a[N];
int dx[10] = {-1, 0, 1, 0};
int dy[10] = {0, 1, 0, -1};
int n, t, p, x, y;
int sum = 0;
signed main()
{
	cin >> n >> t;
	for (int i = 1; i <= n; i++)
	{
		cin >> a[i];
		sum += a[i];
	}
	for (int i = 1; i <= n; i++)
	{
		x += dx[p] * a[i];
		y += dy[p] * a[i]; 
		p = (p + a[i]) % 4;
	}
	p = 0;
	int nx = 0, ny = 0;
	for (int i = 1; i <= t; i++)
	{
		p = (p + (sum % 4)) % 4;
		if (p == 0)
		{
			nx += x;
			ny += y; 
		}
		if (p == 1)
		{
			nx += y; 
			ny -= x; 
		}
		if (p == 2)
		{
			nx -= x; 
			ny -= y; 
		}
		if (p == 3)
		{
			nx -= y; 
			ny += x; 
		}
	} 
	cout << abs(nx) + abs(ny);
	return 0;
}
原文