[agc016b]Colorful Hats 分类讨论

Description

​ 有n个人,每个人都戴着一顶帽子。当然,帽子有不同的颜色。

​ 现在,每个人都告诉你,他看到的所有其他人的帽子共有多少种颜色,请问你有没有符合所有人的描述的情况。

Input

​ 第一行一个整数n。

第二行n个整数,第i个数ai表示第i个人看到的所有其他人帽子颜色的种数。

Output

​ 若存在一种情况满足条件,输出"Yes",否则输出"No"。(均不含引号)

Sample Input

Sample Input 1
3
1 2 2

Sample Input 2
3
1 1 2

Sample Input 3
5
4 3 4 3 4

Sample Input 4
3
2 2 2

Sample Input 5
4
2 2 2 2

Sample Input 6
5
3 3 3 3 3

Sample Output

Sample Output 1
Yes

Sample Output 2
No

Sample Output 3
No

Sample Output 4
Yes

Sample Output 5
Yes

Sample Output 6
No

HINT

2<=n<=10^5

1<=ai<=n-1

Sol

首先我们发现,可以分两类讨论:

1.最大值唯一2.最大值不唯一

如果是情况一,判断要么(mx=n)要么(mx*2<=n),表示每种颜色出现次数是1还是大于1,不满足就是No。

如果是情况二,判断如果(mx-mn>1)那么无解,因为mn的人一定独一无二,mx的人有重复,这样两个人看别的人只是相差为一,否则不会成立。

然后有解的情况是(mx>mncnt),表示至少要算上所有独一无二的颜色以及自己的颜色,然后也要满足(n-mncnt>=(mx-mncnt)*2),表示剩下的每种至少要有两个。

Code

#include <cstdio>
int n,a[100005],ok=1,mx,mn,tot;
int main()
{
    scanf("%d",&n);mn=n+1;
    for(int i=1;i<=n;i++) scanf("%d",&a[i]);
    for(int i=1;i<=n;i++)
    {
        if(a[i]<mn) mn=a[i],tot=1;else if(mn==a[i]) tot++;
        if(a[i]!=mx&&i>1) ok=0;if(a[i]>mx) mx=a[i];
    }
    if(ok) (mx==n-1||(mx!=n-1&&mx*2<=n))?puts("Yes"):puts("No");
    else (mx!=mn+1||(mx==mn+1&&!(mx>tot&&n-tot>=(mx-tot)*2)))?puts("No"):puts("Yes");
}
原文地址:https://www.cnblogs.com/CK6100LGEV2/p/9508038.html