HDU

题目链接:HDU 1205

题意

(N) 种糖果,问是否存在一种排列使得任意连续两颗糖果是不同的糖果。

思路

鸽巢原理前文讲解

设数量最多的糖果数量为 (max),其余糖果的数量为 (s)。把数量最多的糖果看成隔板,可以分隔成 (max - 1) 个空间。

(s<max-1) 时,必然至少有两个隔板之间没有糖果,那么这两个隔板就是相同的糖果,所以无解。否则一定能把 (s) 个糖果隔开 (每次依次放每一种糖果)。

Sample Input

2
3
4 1 1
5
5 4 3 2 1

Sample Output

No
Yes

Code

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1000000 + 10;

ll arr[maxn];

int main() {
    int T;
    scanf("%d", &T);
    while(T--) {
        int n;
        scanf("%d", &n);
        ll maxa = 0;
        ll s = 0;
        for(int i = 0; i < n; ++i) {
            scanf("%lld", &arr[i]);
            maxa = max(maxa, arr[i]);
            s += arr[i];
        }
        s -= maxa;
        if(s >= maxa - 1) printf("Yes
");
        else printf("No
");
    }
    return 0;
}

The desire of his soul is the prophecy of his fate
你灵魂的欲望,是你命运的先知。

原文地址:https://www.cnblogs.com/RioTian/p/14551705.html