【题解】AT2386:[AGC016B] Colorful Hats
作者:互联网
@luogu
手摸几个样例之后我们可以发现,每个人看到的帽子数之差必然<=1
然后我们根据
a
1
.
.
.
a
n
a_1...a_n
a1...an排序
然后
- a 1 = a n a_1=a_n a1=an,意思是,每个人看到的帽子个数相同,那么有两种情况,一种是所有人都不相同,那么 a i a_i ai必须满足 a i = n − 1 a_i=n-1 ai=n−1;或者是帽子情况被分成了几段,每段的个数>1,就是 2 a i < = n 2a_i<=n 2ai<=n。以上两种情况都是yes,否则no
- a 1 + 1 = a n a_1+1=a_n a1+1=an,然后 a i = a 1 a_i=a_1 ai=a1的话,那么 i i i的帽子是独一无二的, a i = a n a_i=a_n ai=an的话,那么 i i i存在另一个人与 i i i帽子颜色相同,所以令 a i = a 1 a_i=a_1 ai=a1的个数为 c n t cnt cnt,满足 c n t < a n 且 2 ( a n − c n t ) < = n − c n t cnt<a_n且2(a_n-cnt)<=n-cnt cnt<an且2(an−cnt)<=n−cnt就是yes,否则no
- a 1 + 1 < a n a_1+1<a_n a1+1<an,直接no
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