Codeforces Round #661 (Div. 3) A. Remove Smallest
You are given the array aa 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 and 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 aa a . The second line of the test case contains n integers (1≤≤100), where 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
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
水,排序后扫一遍判断相邻的数是否相差小于等于1即可。
#include <bits/stdc++.h>
using namespace std;
int a[55];
int main()
{
int t;
cin >> t;
while(t--)
{
int n;
cin >> n;
for(int i = 1; i <= n; i++)
{
cin >> a[i];
}
sort(a + 1, a + n +1);
bool flag = 1;
for(int i = 2; i <= n; i++)
{
if(a[i] > a[i - 1] + 1)
{
cout << "NO" << endl;
flag = 0;
break;
}
}
if(flag) cout << "YES" << endl;
}
return 0;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!