题目链接:https://cn.vjudge.net/contest/174897#problem/A
HOHO,终于从Speakless手上赢走了所有的糖果,是Gardon吃糖果时有个特殊的癖好,就是不喜欢将一样的糖果放在一起吃,喜欢先吃一种,下一次吃另一种,这样;可是Gardon不知道是否存在一种吃糖果的顺序使得他能把所有糖果都吃完?请你写个程序帮忙计算一下。
Input
第一行有一个整数T,接下来T组数据,每组数据占2行,第一行是一个整数N(0,1000000】,第二行是N个数,表示N种糖果的数目Mi(0,1000000】。
Output
对于每组数据,输出一行,包含一个"Yes"或者"No"。
Sample Input
2
3
4 1 1
5
5 4 3 2 1
Sample Output
No
Yes
题解:先找出最大数目的那种糖果,设数目为n,利用抽屉原理,先把这n个隔空排列,那么中间有n-1个空挡,这样如果剩下的糖果总数大于等于n-1,就符合条件,反之,则不然
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<iostream>
int a[1000001];
using namespace std;
int main()
{
int t,n,i,s,mmax;
scanf("%d",&t);
while(t--)
{
mmax=-999;
s=0;
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
mmax=max(mmax,a[i]);
}
for(i=0;i<n;i++)
{
if(a[i]!=mmax)
s+=a[i];
if(s>=mmax-1)//大于等于最多的那个就跳出,这样i值一定小于n
break;
}
if(i==n)
printf("No\n");
else
printf("Yes\n");
}
return 0;
}