AtCoder Grand Contest 016 B

B - Colorful Hats


Time limit : 2sec / Memory limit : 256MB

Score : 700 points

Problem Statement

There are N cats. We number them from 1 through N.

Each of the cats wears a hat. Cat i says: "there are exactly ai different colors among the N−1 hats worn by the cats except me."

Determine whether there exists a sequence of colors of the hats that is consistent with the remarks of the cats.

Constraints

  • 2≤N≤105
  • 1≤aiN−1

Input

Input is given from Standard Input in the following format:

N
a1 a2  aN

Output

Print Yes if there exists a sequence of colors of the hats that is consistent with the remarks of the cats; print No otherwise.


Sample Input 1

Copy
3
1 2 2

Sample Output 1

Copy
Yes

For example, if cat 12 and 3 wears red, blue and blue hats, respectively, it is consistent with the remarks of the cats.


Sample Input 2

Copy
3
1 1 2

Sample Output 2

Copy
No

From the remark of cat 1, we can see that cat 2 and 3 wear hats of the same color. Also, from the remark of cat 2, we can see that cat 1 and 3 wear hats of the same color. Therefore, cat 1 and 2 wear hats of the same color, which contradicts the remark of cat 3.


Sample Input 3

Copy
5
4 3 4 3 4

Sample Output 3

Copy
No

Sample Input 4

Copy
3
2 2 2

Sample Output 4

Copy
Yes

Sample Input 5

Copy
4
2 2 2 2

Sample Output 5

Copy
Yes

Sample Input 6

Copy
5
3 3 3 3 3

Sample Output 6

Copy
No

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<algorithm>
#include<iostream>
#include<queue>
#include<map>
#include<cmath>
#include<set>
#include<stack>
#define ll long long
#define pb push_back
#define max(x,y) ((x)>(y)?(x):(y))
#define min(x,y) ((x)>(y)?(y):(x))
#define cls(name,x) memset(name,x,sizeof(name))
using namespace std;
const int inf=1e9+10;
const int maxn=1e6+10;
const int maxm=20;
const int mod=1e9+7;
const double pi=acos(-1.0);
int n;
int num[maxn];
int main()
{
    //freopen("in.txt","r",stdin);
    while(~scanf("%d",&n))
    {
        int minnum=inf,maxnum=0;
        for(int i=0;i<n;i++)
        {
            scanf("%d",&num[i]);
            minnum=min(minnum,num[i]);
            maxnum=max(maxnum,num[i]);
        }
        if(!(minnum+1==maxnum||minnum==maxnum))
        {
            printf("No
");
            continue;
        }
        int color=maxnum;
        int maxc=0;
        for(int i=0;i<n;i++)
        {
            if(maxnum-1==num[i]) color--;
            else maxc++;
        }
        if(minnum+1==maxnum)
        {
            if(maxc>=color*2&&color>=1)
                printf("Yes
");
            else printf("No
");
        }
        else
        {
            if(maxc>=color*2||maxc==color+1)
                printf("Yes
");
            else printf("No
");
        }
    }
    return 0;
}


原文地址:https://www.cnblogs.com/mgz-/p/7081409.html