【CodeForces - 1660B 】Vlad and Candies(简单写法)

 Vlad and Candies

CodeForces - 1660B 

Not so long ago, Vlad had a birthday, for which he was presented with a package of candies. There were nn types of candies, there are a_iai​ candies of the type ii (1 \le i \le n1≤i≤n).

Vlad decided to eat exactly one candy every time, choosing any of the candies of a type that is currently the most frequent (if there are several such types, he can choose any of them). To get the maximum pleasure from eating, Vlad does not want to eat two candies of the same type in a row.

Help him figure out if he can eat all the candies without eating two identical candies in a row.

Input

The first line of input data contains an integer tt (1 \le t \le 10^41≤t≤104) — the number of input test cases.

The following is a description of tt test cases of input, two lines for each.

The first line of the case contains the single number nn (1 \le n \le 2 \cdot 10^51≤n≤2⋅105) — the number of types of candies in the package.

The second line of the case contains nn integers a_iai​ (1 \le a_i \le 10^91≤ai​≤109) — the number of candies of the type ii.

It is guaranteed that the sum of nn for all cases does not exceed 2 \cdot 10^52⋅105.

Output

Output tt lines, each of which contains the answer to the corresponding test case of input. As an answer, output "YES" if Vlad can eat candy as planned, and "NO" otherwise.

You can output the answer in any case (for example, the strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive answer).

Sample 1

InputcopyOutputcopy
6
2
2 3
1
2
5
1 6 2 4 3
4
2 2 2 1
3
1 1000000000 999999999
1
1
YES
NO
NO
YES
YES
YES

Note

In the first example, it is necessary to eat sweets in this order:

  • a candy of the type 22, it is the most frequent, now a = [2, 2]a=[2,2];
  • a candy of the type 11, there are the same number of candies of the type 22, but we just ate one, now a = [1, 2]a=[1,2];
  • a candy of the type 22, it is the most frequent, now a = [1, 1]a=[1,1];
  • a candy of the type 11, now a = [0, 1]a=[0,1];
  • a candy of the type 22, now a = [0, 0]a=[0,0] and the candy has run out.

In the second example, all the candies are of the same type and it is impossible to eat them without eating two identical ones in a row.

In the third example, first of all, a candy of the type 22 will be eaten, after which this kind will remain the only kind that is the most frequent, and you will have to eat a candy of the type 22 again.

题意描述:多实例,给多组数据,第一行表示糖果种类,第二行表示糖果的个数,弗拉德现在要吃掉这些糖果,他每次都会吃糖果数最多的糖果,但是他不想连续吃掉两个相同类型的糖果,就是如果某个糖果个数连续两次都是最大的话就输出NO(包括只有一种糖果但是糖果数量大于1的时候),也就是单独列出糖果种类只有1种的时候的情况:多于1NO,等于1YES;然后如果糖果种类多的时候:最多的和倒数第二多的糖果之间相差大于1的时候就是NO(因为只要糖果相差小于等于1,一定可以至少对这两种糖果进行循环吃),其它都是YES。

解题思路:用数组存糖果个数,然后sort排序求糖果最多和倒数第二多的糖果,判断一下,最后根据情况输出。

AC

#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<iostream>
int a[201000];
using namespace std;
int main(void)
{
	int t,n;
	cin>>t;
	while(t--)
	{
		cin>>n;
		for(int i=0;i<n;i++)
		{
			cin>>a[i];
		}
		sort(a,a+n);
		if(a[n-1]-a[n-2]>=2)
		{
			cout<<"NO"<<endl;
		}
			
		else if(n==1&&a[0]>1)
			cout<<"NO"<<endl;
		else
			cout<<"YES"<<endl; 
	}
	return 0;
 } 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值