A. Remove Smallest
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
You are given the array aa consisting of nn positive (greater than zero) integers.
In one move, you can choose two indices ii and jj (i≠ji≠j) such that the absolute difference between aiai and ajaj is no more than one (|ai−aj|≤1|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 tt independent test cases.
Input
The first line of the input contains one integer tt (1≤t≤10001≤t≤1000) — the number of test cases. Then tt test cases follow.
The first line of the test case contains one integer nn (1≤n≤501≤n≤50) — the length of aa. The second line of the test case contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤1001≤ai≤100), where aiai is the ii-th element of aa.
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
Copy
5 3 1 2 2 4 5 5 5 5 3 1 2 4 4 1 3 4 4 1 100
output
Copy
YES YES NO NO YES
Note
In the first test case of the example, we can perform the following sequence of moves:
- choose i=1i=1 and j=3j=3 and remove aiai (so aa becomes [2;2][2;2]);
- choose i=1i=1 and j=2j=2 and remove ajaj (so aa becomes [2][2]).
In the second test case of the example, we can choose any possible ii and jj 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 22 and 44.
题意:每次选两个数,如果这两个数差的绝对值小于等于1,则删掉小的那个,如果相等,则随便删一个,问是否能删剩下一个数。
思路:排序,然后逐个遍历看差是否小于等于1.
ac代码:
#include<iostream>
#include<string>
#include<string.h>
#include<cstring>
#include<algorithm>
#include<queue>
#include<stack>
#include<math.h>
#define MAXN 100005
using namespace std;
bool cmp(int a,int b){
return a<b;
}
int main(){
int t;
cin>>t;
while(t--){
int n;
int num[1005];
cin>>n;
for(int i=0;i<n;i++){
cin>>num[i];
}
sort(num,num+n,cmp);
int flag=true;
for(int i=1;i<n;i++){
if(!(num[i-1]+1>=num[i])){
flag=false;
break;
}
}
if(flag){
cout<<"YES"<<endl;
}
else {
cout<<"NO"<<endl;
}
}
}