codeforces (C++ Doremy‘s Paint 3)

题目: 

c10b3705881242d4be4ec774702478cd.png f1f2db08c73d4f65b1a05a13c5b56a9f.png

翻译:

c86da8720a8f496ca15a915f9b62cf9e.jpeg 

思路:

 1、题目意思:将数组中的数进行排列,任意相邻两个数的和都相等,才能说这个数组为好。一下分三种情况讨论。

2、当数组中有三种及三种以上的数字,那任意相邻两个数的和都相等必然无法成立。

3、当数组中只有一种数字的时候,则必然是好数组。

4、当数组中有两种数字就统计两个数字出现的个数,个数绝对值差1或者0,即可以排列形成好数组。

代码: 

#include<iostream>
#include<map>
#include<cmath>
using namespace std;
int main()
{
	int t;
	cin >> t;
	while (t--)
	{
		int n;
		cin >> n;
		map<int,int>mp ;
		int flag = 0;
		for (int i = 0; i < n; i++)
		{
			int tep;
			cin >> tep;
			mp[tep]++;
		}
		if (mp.size() > 2)
			cout << "NO" << endl;
        else if(mp.size()==1)
            cout<< "YES" << endl;
		else if(mp.size()==2)
		{
			int a[2],k=0;
			for (auto i : mp)
			{
				a[k++] = i.second;
			}
			flag = abs(a[0] - a[1]);
			if (flag == 1||flag==0)
				cout << "YES" << endl;
			else
				cout << "NO" << endl;
		}
	}
}

结果: 23466eee5e4741c587b6262effb12ab8.png

猜你喜欢

转载自blog.csdn.net/qq_74156152/article/details/134109673
今日推荐