1506D Epic Transformation(思维、贪心)

题目

思路:假设有只有一种数字,答案为该数字数量,两种数字,答案为大的数量减去小的数量,三种数字,假设数量从小到大排序 s1 s2 s3 ,可以知道当s1+s2<=s3, 答案为s3-(s1+s2) 当s1+s2>s3 ,如果(s1+s2+s3)%20,答案为0,(s1+s2+s3)%21,答案为1(这里好好想想应该不难,cf有一道800分的题就是只有三个数的)。然后对于数字种数为3个以上的情况,其实都可以转化为3个的情况。

Code:

#include<iostream>
#include<map>
#include<algorithm>
using namespace std;
const int Max = 1e6 + 5;
int lst[Max];
int main()
{
    
    
	int t;cin >> t;
	while (t--)
	{
    
    
		int n;cin >> n;
		map<int, int> ma;
		for (int i = 1;i <= n;i++)
		{
    
    
			int a;cin >> a;
			ma[a]++;
		}
		int g = 0;

		for (auto i = ma.begin();i != ma.end();i++)	lst[++g] = i->second;
		sort(lst + 1, lst + 1 + g);

		if (ma.size() == 1)	cout << ma.begin()->second << endl;
		else if (ma.size() == 2) cout << lst[2] - lst[1] << endl;
		else
		{
    
    
			int sum = 0;
			for (int i = 1;i <= g - 1;i++)	sum += lst[i];
			if (sum >= lst[g])
			{
    
    
				if ((sum + lst[g]) % 2 == 0)cout << 0 << endl;
				else cout << 1 << endl;
			}
			else cout << lst[g] - sum << endl;
		}
	}
}

猜你喜欢

转载自blog.csdn.net/asbbv/article/details/115234158