zoukankan      html  css  js  c++  java
  • [思维]Colorful Hats

    题目描述

    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≤ai≤N−1

    输入

    Input is given from Standard Input in the following format:

    N
    a1 a2 … aN

    输出

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

    样例输入

    3
    1 2 2
    

    样例输出

    Yes
    

    提示

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

    假设共有m种颜色,当猫的颜色是独一无二时,它能看到其他m-1种颜色;否则,能看到所有m种颜色;
    当a[1]=a[2]=..=a[n]=m时,若m==n-1,输出Yes;若m<n-1,当2*m<=n时输出Yes,否则输出No
    当a[1],a[2],..,a[n]不全相等时,若最大与最小相差超过1,输出No;否则m=max{a[i]},m种颜色中独一无二的有s=cnt{a[i]==m-1},当s<=m&&2*(m-s)<=n-s时输出Yes,否则输出No
    #include <iostream>
    #include<cstdio>
    #include<cstring>
    #include<cmath>
    #include<algorithm>
    using namespace std;
     
    int n,a[100005];
     
    int check(){
      for(int i=2;i<=n;i++){
        if(a[i]!=a[1]) return -1;
      }
      return a[1];
    }
     
    int main()
    {
        scanf("%d",&n);
        int m=0,mi=n;
        for(int i=1;i<=n;i++) scanf("%d",&a[i]),m=max(m,a[i]),mi=min(mi,a[i]);
        if(m-mi>=2) return puts("No")*0;
        int tmp=check();
        if(tmp!=-1){
            if(tmp==n-1) return puts("Yes")*0;
            else if(2*tmp<=n) return puts("Yes")*0;
            else return puts("No")*0;
        }
        int s=0;
        for(int i=1;i<=n;i++) if(a[i]==m-1) s++;
        if(s<m&&2*(m-s)<=n-s) return puts("Yes")*0;
        return puts("No")*0;
    }
    View Code
  • 相关阅读:
    redis的两种安装方法
    http头部信息
    软件测试技术知识
    github使用
    3 python之基础概要
    2 Python之编程语言介绍及变量
    1 Python入门及计算机硬件知识
    Linux常用命令
    CMDB小练习
    CMDB和运维自动化
  • 原文地址:https://www.cnblogs.com/lllxq/p/10499966.html
Copyright © 2011-2022 走看看