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
inputCopy
5
3
1 2 2
4
5 5 5 5
3
1 2 4
4
1 3 4 4
1
100
outputCopy
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.
div3前3个是入门级别,到后面就不会了。我是入门级别。
这题是一个排序判断差分的水题。

#include
#include
#include
#include
#include
using namespace std;
#define pairs pair
const int maxn=1e6+1;
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int n;
        cin>>n;
        multiset<int>q;
        for(int i=1;i<=n;i++)
        {
            int x;
            cin>>x;
            q.insert(x);
        }
        auto k=q.begin(),p=q.begin();
        k++;
        int flag=false;
        for(;k!=q.end();k++,p++)
        {
            if(*k-*p<=1)continue;
            else flag=true;
        }
        if(flag)
            cout<<"NO"<<endl;
        else cout<<"YES"<<endl;
    }
}

你可能感兴趣的:(水题)