CF1399A Remove Smallest

原题链接:http://codeforces.com/problemset/problem/1399/A

Remove Smallest

You are given the array a consisting of n positive (greater than zero) integers.

In one move, you can choose two indices i and j (i≠j) such that the absolute difference between ai and aj is no more than one (|ai−aj|≤1) and remove the smallest of these two elements. If two elements are equal, you can remove any of them (but exactly one).

Your task is to find if it is possible to obtain the array consisting of only one element using several (possibly, zero) such moves or not.

You have to answer t independent test cases.

Input

The first line of the input contains one integer t (1≤t≤1000) — the number of test cases. Then t test cases follow.

The first line of the test case contains one integer n (1≤n≤50) — the length of a. The second line of the test case contains n integers a1,a2,…,an (1≤ai≤100), where ai is the i-th element of a.

Output

For each test case, print the answer: “YES” if it is possible to obtain the array consisting of only one element using several (possibly, zero) moves described in the problem statement, or “NO” otherwise.

Example
input

5
3
1 2 2
4
5 5 5 5
3
1 2 4
4
1 3 4 4
1
100

output

YES
YES
NO
NO
YES

Note

In the first test case of the example, we can perform the following sequence of moves:

choose i=1 and j=3 and remove ai (so a becomes [2;2]);
choose i=1 and j=2 and remove aj (so a becomes [2]).
In the second test case of the example, we can choose any possible i and j any move and it doesn’t matter which element we remove.

In the third test case of the example, there is no way to get rid of 2 and 4.

题目大意

t t t组询问,每组给一个 n n n以及由 n n n个数字组成的数列。

每次操作可以选择任意两个绝对值只差 ≤ 1 \le 1 1的数字,去掉其中较小的一个,若两数字相同则去掉任意一个。

问这个数列最后能否被消除到只剩一个数。

题解

时隔一年半再 O I OI OI,爷青回。

想要消掉最小的那个数 a a a,必须要找一个 b b b满足 a ≤ b ≤ a + 1 a\le b\le a+1 aba+1,在消掉 a a a以后, b b b就变成了最小的数,于是以此类推,只要对于当前最小值 a a a找不到符合要求的 b b b,这个数列就不可能被消到只剩一个。

要实现的话,排个序就好了。

代码

退役老咸鱼含泪复习了 s c a n f   p r i n t f   m e m s e t scanf\ printf\ memset scanf printf memset以及 s o r t sort sort的用法。

#include
using namespace std;
int t,a;
int num[55];
void in()
{
	scanf("%d",&a);memset(num,105,sizeof(num));
	for(int i=1;i<=a;++i)scanf("%d",&num[i]);
}
void ac()
{
	sort(num+1,num+1+a);
	for(int i=1;i<a;++i)if(num[i+1]-num[i]>1){printf("NO\n");return;}
	printf("YES\n");
}
int main()
{
	scanf("%d",&t);
	for(int i=1;i<=t;++i){in(),ac();}
} 

你可能感兴趣的:(杂============,贪心)