hdu 1205 分糖果-排列

题目链接:This is the link

吃糖果

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 47492    Accepted Submission(s): 13557


 

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  

Author

Gardon

 

Source

Gardon-DYGG Contest 2

 

Recommend

JGShining   |   We have carefully selected several similar problems for you:  1201 1021 1108 1008 1005 

数学分析题目:

       这是一道排列问题,就是将若干种类的糖果按要求排列,相同种类的糖果不能相邻。这就要考虑插孔法。先考虑数量最多的那一种糖果(假设这种糖果有n个)先间隔排好,留出n-1个空格。剩下的糖果总数大于等于n-1,就可以使得数量最多的那一种糖果不会相邻,再将剩下的糖果按种类依次插入之前已经排好糖果的空隙中,则可以满足题目的要求,输出yes。如果剩下的糖果总数小于n-1,数量最多的那一种糖果一定会出现相邻的状况,因此输出no。

//注:这篇博客告诉自己,开放思维,不要第一眼看见题目就去想用什么算法,先考虑简单的数学关系,可能题目很简单

//不需要用数组做

This is the code

#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<iostream>
#include <iomanip>
#include<list>
#include<map>
#include<queue>
#include<sstream>
#include<stack>
#include<string>
#include<set>
#include<vector>
using namespace std;
#define PI acos(-1.0)
#define EPS 1e-8
#define MOD 1e9+7
#define LL long long
#define ULL unsigned long long     //1844674407370955161
#define INT_INF 0x7f7f7f7f      //2139062143
#define LL_INF 0x7f7f7f7f7f7f7f7f //9187201950435737471
const int dr[]={0, 0, -1, 1, -1, -1, 1, 1};
const int dc[]={-1, 1, 0, 0, -1, 1, -1, 1};
// ios::sync_with_stdio(false);
// 那么cin, 就不能跟C的 scanf,sscanf, getchar, fgets之类的一起使用了。
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n,maxn=0;
        LL sum=0;
        scanf("%d",&n);
        for(int i=0;i<n;++i)
        {
            int temp;
            scanf("%d",&temp);
            maxn=max(maxn,temp);//寻找最多的
            sum+=temp;//累加和
        }
        sum-=maxn;
        if(sum>=maxn-1)
            puts("Yes");//输出字符串,自动换行
        else
            puts("No");
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/sdau_fangshifeng/article/details/81700084