HDU 1205 吃糖果(想想题)

题目传送:http://acm.hdu.edu.cn/showproblem.php?pid=1205

Problem Description
HOHO,终于从Speakless手上赢走了所有的糖果,是Gardon吃糖果时有个特殊的癖好,就是不喜欢将一样的糖果放在一起吃,喜欢先吃一种,下一次吃另一种,这样;可是Gardon不知道是否存在一种吃糖果的顺序使得他能把所有糖果都吃完?请你写个程序帮忙计算一下。
Input
第一行有一个整数T,接下来T组数据,每组数据占2行,第一行是一个整数N(0<N<=1000000),第二行是N个数,表示N种糖果的数目Mi(0<Mi<=1000000)。
Output
对于每组数据,输出一行,包含一个"Yes"或者"No"。
Sample Input
2
3
4 1 1
5
5 4 3 2 1
Sample Output
No
Yes
 
启发博客:http://www.cnblogs.com/vivider/p/3697688.html
考虑两种糖果,假设其中一种最大数量为n,要想这两种糖果交替吃完,则另一种糖果最少的数量为n-1.因为n个糖果排成一排,内部一共有n-1个空,分离相邻的两个相同的糖果。回到题目中,找出某种糖果的最大数量maxvalue,至少需要maxvalue-1个糖果(即除去最大数量剩下的糖果)去填充空隙。如果剩下的糖果数大于maxvalue,也是可以的,因为我们可以想成把数量较少的糖果用来“加厚”原来的空隙,即可以两个或两个以上不同的糖果去填充同一个空隙。
 
 1 #include<cstdio>
 2 #include<iostream>
 3 #include<algorithm>
 4 #include<cstring>
 5 #include<string.h>
 6 #include<cmath>
 7 using namespace std;
 8 #define N 1000005
 9 
10 int a[N];
11 
12 int main()
13 {
14     
15     int T;
16     scanf("%d",&T);
17     int n,i;
18     long long sum;
19     while(T--)
20     {
21         scanf("%d",&n);
22         sum=0;
23         for(i=0;i<n;i++)
24             scanf("%d",&a[i]);
25         sort(a,a+n);
26         for(i=0;i<n-1;i++)
27             sum+=a[i];
28         if(sum>=a[n-1]-1)
29             printf("Yes
");
30         else 
31             printf("No
");
32     }
33     return 0;
34 }
 
原文地址:https://www.cnblogs.com/Annetree/p/7095320.html