传送门
分析
这道题的意思大概是说有一串数字,你每次可以取出两个数字x,y,但要求是x和y相差不能超过1,然后你可以任意删去一个数字,这种操作可以进行无数次。
给你一个数组,然后让你判断能不能通过这种操作(或者一次不做),让这串数组仅由相同的数字组成
首先我们要确定一下,任何一个数字如果要删除,只能有三种情况:选择两个相等的数字删去一个,选择两个相差1的数字删去小的那一个,选择两个相差1的数字删去大的那一个。
我们可以选择对每个数字进行第一种操作,这样可以保证数列中每种数字只会出现一次,这样接下来的操作只能针对两个不同数字之间进行
然后我们将数组从小到大排列,遍历每一个数字,将比他小1的数字的值赋为0(提前用map处理好),最后再遍历一遍数组,如果发现有两个数字还存在在数组中,就说明其中有一个数字无法消除,输出NO
代码
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <queue>
#include <cstring>
#include <unordered_map>
#include <set>
using namespace std;
const int N = 55;
int n;
int main(){
int T;
scanf("%d",&T);
while(T--){
scanf("%d",&n);
int x,num = 0;
unordered_map<int,int> m;
set<int> s;
for(int i = 0;i < n;i++){
scanf("%d",&x);
m[x]++;
s.insert(x);
}
int flag = 1;
for(auto p:s){
m[p - 1] = 0;
}
for(auto p:s){
if(m[p] > 0)
num++;
}
if(num == 1 || !num) puts("YES");
else puts("NO");
}
}