我是靠谱客的博主 靓丽酒窝,最近开发中收集的这篇文章主要介绍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<iostream>
#include<cstdio>
#include<vector>
#include<queue>
#include<set>
using namespace std;
#define pairs pair<int,int>
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;
}
}

最后

以上就是靓丽酒窝为你收集整理的A. Remove Smallest的全部内容,希望文章能够帮你解决A. Remove Smallest所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(66)

评论列表共有 0 条评论

立即
投稿
返回
顶部