其他分享
首页 > 其他分享> > 【题解】AT2386:[AGC016B] Colorful Hats

【题解】AT2386:[AGC016B] Colorful Hats

作者:互联网

@luogu
手摸几个样例之后我们可以发现,每个人看到的帽子数之差必然<=1
然后我们根据 a 1 . . . a n a_1...a_n a1​...an​排序
然后

Code:

#include <bits/stdc++.h>
#define maxn 100010
using namespace std;
int a[maxn], n;

inline int read(){
	int s = 0, w = 1;
	char c = getchar();
	for (; !isdigit(c); c = getchar()) if (c == '-') w = -1;
	for (; isdigit(c); c = getchar()) s = (s << 1) + (s << 3) + (c ^ 48);
	return s * w;
}

int main(){
	n = read();
	for (int i = 1; i <= n; ++i) a[i] = read();
	sort(a + 1, a + 1 + n);
	if (a[n] > a[1] + 1) return puts("No"), 0;
	if (a[1] == a[n]){
		if (a[1] * 2 <= n || a[1] + 1 == n)
			puts("Yes");
		else puts("No");
	} else{
		int cnt = 0;
		for (int i = 1; i <= n; ++i)
			if (a[i] == a[1])
				++cnt;
			else break;
		if (cnt < a[n] && (a[n] - cnt) * 2 <= (n - cnt))
			puts("Yes");
		else puts("No");
	}
	return 0;
}

标签:cnt,帽子,Colorful,int,题解,Hats,a1,ai,getchar
来源: https://blog.csdn.net/ModestCoder_/article/details/110562031