C. Similar Pairs

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

We call two numbers xx and yy similar if they have the same parity (the same remainder when divided by 22), or if |x−y|=1|x−y|=1. For example, in each of the pairs (2,6)(2,6), (4,3)(4,3), (11,7)(11,7), the numbers are similar to each other, and in the pairs (1,4)(1,4), (3,12)(3,12), they are not.

You are given an array aa of nn (nn is even) positive integers. Check if there is such a partition of the array into pairs that each element of the array belongs to exactly one pair and the numbers in each pair are similar to each other.

For example, for the array a=[11,14,16,12]a=[11,14,16,12], there is a partition into pairs (11,12)(11,12) and (14,16)(14,16). The numbers in the first pair are similar because they differ by one, and in the second pair because they are both even.

Input

The first line contains a single integer tt (1≤t≤10001≤t≤1000) — the number of test cases. Then tt test cases follow.

Each test case consists of two lines.

The first line contains an even positive integer nn (2≤n≤502≤n≤50) — length of array aa.

The second line contains nn positive integers a1,a2,…,ana1,a2,…,an (1≤ai≤1001≤ai≤100).

Output

For each test case print:

  • YES if the such a partition exists,
  • NO otherwise.

The letters in the words YES and NO can be displayed in any case.

Example

input

Copy

7
4
11 14 16 12
2
1 8
4
1 1 1 1
4
1 2 5 6
2
12 13
6
1 6 3 10 5 8
6
1 12 3 10 5 8

output

Copy

YES
NO
YES
YES
YES
YES
NO

Note

The first test case was explained in the statement.

In the second test case, the two given numbers are not similar.

In the third test case, any partition is suitable.

题意:给你一个偶数n数组然后判断是否存在n/2数对满足绝对值相差一或者两个数都是奇数或者都是偶数。

解题思路:在输入数组的过程中记录奇数偶数的个数如果都是奇数或者都是偶数直接输出“YES”,如果不满足则把数组按升序排个序,并且定义一个标记数组用来标记该数有没有被使用过,然后先找出绝对值相差一的所有数对,并记录对数。接下来只要判断减去绝对值相差一的数对后,奇数数和偶数数的个数是否能被2整除。

Ac代码:

#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
int a[55],vis[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);
int cnt1=0;int cnt0=0;int cnt=0;
for(int i=1;i<=n;i++){
if(a[i]%2) cnt1++;//记录奇数个数;
else cnt0++; //记录偶数个数;
}
if(cnt1==0) cout<<"YES"<<endl; //判断特殊情况;
else if(cnt0==0) cout<<"YES"<<endl;
else{
for(int i=1;i<=n;i++) vis[i]=0; //别忘记标记数组初始化;
for(int i=1;i<n;i++){
for(int j=i+1;j<=n;j++){
if(a[j]-a[i]==1&&vis[j]==0){cnt++;vis[j]=1;break;}//找绝对值相差一的数对;
}
}
int k=0;
for(int i=0;i<=cnt;i++){
cnt1-=i;
cnt0-=i;
if(cnt1%2==0&&cnt0%2==0) {k=1;cout<<"YES"<<endl;break;}//判断奇数数和偶数数是否能被2整除;
}
if(!k) cout<<"NO"<<endl;
}
}
return 0;
}

最新文章

  1. 新版markdown功能发布!支持github flavored markdown!
  2. 自动登录VSS
  3. Code First 中使用 ForeignKey指定外键时总是显示未引用
  4. dancing link 学习资源导航+心得
  5. git Unstaged changes after reset
  6. CSS在不同浏览器兼容问题,margin偏移/offset溢出等
  7. Expectation Maximization and GMM
  8. ANDROID_MARS学习笔记_S04_008_用Listview、自定义adapter显示返回的微博数据
  9. FTP文件操作之上传文件
  10. Struts框架2
  11. java定义一个二维数组
  12. TStrings (TStringList)很有功能
  13. 在delphi中我用DBGrid选择多条记录,如何一次把选择的多条记录删掉
  14. sigint sigterm 有什么区别啊
  15. Java.lang的研究(分析包含的重要类和接口)
  16. cmake window下 sh.exe was found in your PATH, here
  17. Android中获取资源的id和url方法总结
  18. supervisor运行virtualenv环境下的nagios-api
  19. Linux SNMP 监控一些常用OID
  20. 使用Faric+Git进行分布式代码管理

热门文章

  1. List遍历以及剔除指定数据
  2. HDU 6390 GuGuFishtion(莫比乌斯反演 + 欧拉函数性质 + 积性函数)题解
  3. vue 二级子路由跳转不了 bug
  4. Battery API All In One
  5. Swift 5.3 All In One
  6. React Hooks: useEffect All In One
  7. DRM &amp; 音视频 &amp; 防盗链
  8. cloud linux cli &amp; dart pub
  9. SameSite cookies explained
  10. css-next &amp; grid layout