zoukankan      html  css  js  c++  java
  • 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;
    }


  • 相关阅读:
    (转)iOS-Runtime知识点整理
    iOS开发--SQLite重要框架FMDB的使用
    iOS开发--数据库管理CoreData的使用
    iOS超全开源框架、项目和学习资料汇总--数据库、缓存处理、图像浏览、摄像照相视频音频篇
    【导航条滚动透明】一个分类搞定
    成熟的程序员应该掌握的4个知识点
    iOS开发之浅谈MVVM的架构设计与团队协作
    Leetcode-One Edit Distance
    Leetcode-Read N Characters Given Read4 II
    Leetcode-Read N Characters Given Read4
  • 原文地址:https://www.cnblogs.com/mgz-/p/7081409.html
Copyright © 2011-2022 走看看