天天看點

HDU 1205 吃糖果(數學題)

吃糖果

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)

Total Submission(s): 32791    Accepted Submission(s): 9303

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      
Hint
    
    Hint      

Please use function scanf

題解:最多那堆糖果<= 剩餘的+1 ,即輸出Yes,否則輸出No。。。

AC代碼:

#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<string>
#include<cstdlib>
#include<algorithm>
typedef long long LL;
using namespace std;
#define max1 1000001
int a[max1]={0};
int main()
{
    __int64 sum;
    int N;
    int T;
    int i = 0;
    int max = 0;
    scanf("%d", &N);
    while(N--)
    {
        scanf("%d", &T);
        {
            i = 0;
            max = 0;
            sum = 0;
            while(T--)
            {
                scanf("%d", a+i);
                sum += a[i];
                max = a[i]>max? a[i]: max;  //求出最大值 
                i++;
            }
            if( max<=(sum - max + 1 )) //最多那堆<= 剩餘的+1 
                printf("Yes\n");
            else
                printf("No\n");
        }
    }

    return 0;
}