AtCoder Grand Contest 016 B

时间限制: 1 Sec  内存限制: 128 MB
提交: 72  解决: 18
[提交] [状态] [命题人:admin]

题目描述

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.

来源/分类

 
可以证明,ai的最大值与最小值至多相差1,设共有col种颜色
1.ai自己一个颜色,则ai = col-1
2.有其他猫和ai同一个颜色,则ai = col
 
取ai的最大值max,最小值min。
1.当max = col-1时,此时所有的ai相等,均为col-1,cnt[max] = n,max = n-1。
2.当max = col时,cnt[max]+cnt[min] = n因为min = col-1,所以这m个猫颜色各不相同。而剩下的猫中,每种颜色都至少有两只,则这些猫的颜色col2,1<=col2<=(n-cnt[min])/2,所以cnt[min]<col<=cnt[min]+(n-cnt[min])/2.
 
 
 
#include "bits/stdc++.h"

using namespace std;
const int maxn = 1e5 + 100;

int cnt[maxn];

int main() {
    //freopen("input.txt", "r", stdin);
    int n;
    cin >> n;
    int x, m = -1;
    for (int i = 0; i < n; i++) {
        cin >> x;
        cnt[x]++;
        m = max(m, x);
    }
    if ((cnt[m] == n && m == n - 1) ||
        (cnt[m] + cnt[m - 1] == n && cnt[m - 1] <m &&
         (m - cnt[m - 1]) * 2 <= cnt[m]))
        cout << "Yes" << endl;
    else
        cout << "No" << endl;
    return 0;
}
原文地址:https://www.cnblogs.com/albert-biu/p/10517961.html