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;
}

CodeForces - 1360C的更多相关文章

  1. python爬虫学习(5) —— 扒一下codeforces题面

    上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...

  2. 【Codeforces 738D】Sea Battle(贪心)

    http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...

  3. 【Codeforces 738C】Road to Cinema

    http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...

  4. 【Codeforces 738A】Interview with Oleg

    http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...

  5. CodeForces - 662A Gambling Nim

    http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...

  6. CodeForces - 274B Zero Tree

    http://codeforces.com/problemset/problem/274/B 题目大意: 给定你一颗树,每个点上有权值. 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连 ...

  7. CodeForces - 261B Maxim and Restaurant

    http://codeforces.com/problemset/problem/261/B 题目大意:给定n个数a1-an(n<=50,ai<=50),随机打乱后,记Si=a1+a2+a ...

  8. CodeForces - 696B Puzzles

    http://codeforces.com/problemset/problem/696/B 题目大意: 这是一颗有n个点的树,你从根开始游走,每当你第一次到达一个点时,把这个点的权记为(你已经到过不 ...

  9. CodeForces - 148D Bag of mice

    http://codeforces.com/problemset/problem/148/D 题目大意: 原来袋子里有w只白鼠和b只黑鼠 龙和王妃轮流从袋子里抓老鼠.谁先抓到白色老鼠谁就赢. 王妃每次 ...

随机推荐

  1. 牛客多校第九场 && ZOJ3774 The power of Fibonacci(二次剩余定理+斐波那契数列通项/循环节)题解

    题意1.1: 求\(\sum_{i=1}^n Fib^m\mod 1e9+9\),\(n\in[1, 1e9], m\in[1, 1e4]\) 思路1.1 我们首先需要知道斐波那契数列的通项是:\(F ...

  2. sql-libs(6) 双引号的报错注入

    payload:http://192.168.48.130/sqli-laaess-6/?id=1" and updatexml(1,concat(0x7e,(SELECT schema_n ...

  3. taro 进阶指南

    taro 进阶指南 配置 https://nervjs.github.io/taro/docs/config.html https://nervjs.github.io/taro/docs/confi ...

  4. Fast-RTPS简介

    RTPS即DDS中的主要核心通信部分.它提供实时高效的去中心化publish/subscribe通信机制.是ROS-2的核心底层通信组件,也是未来机器人/无人驾驶领域一个必然的方向. 资料参考: ht ...

  5. MacOS下PHP7.1升级到PHP7.4.15

    最近写SDK的时候需要用到object类型提示符,PHPStorm智能提示说需要PHP7.2以上才能支持这种类型提示. 我一查我本机的PHP是7.1.30版本,于是考虑升级一下PHP版本. 首先要尝试 ...

  6. Error running 'tomcat': Unknown error

    免费分享95套java实战项目,不仅有源码还有对应的开发视频,关注公众号『勾玉技术』回复"95"即可获取 无意中发现了一位清华大佬的算法笔记,需要的加公众号 勾玉技术 回复 清华算 ...

  7. .Net -- NLog日志框架配置与使用

    NLog是适用于各种.NET平台(包括.NET标准)的灵活,免费的日志记录平台,NLog可将日志写入多个目标,比如Database.File.Console.Mail.下面介绍下NLog的基本使用方法 ...

  8. 实现 Abp Vnext Pro

    Abp Vnext Pro 的 Vue 实现版本 开箱即用的中后台前端/设计解决方案 知识点 .Net Core5.0 Abp Vnext 4.x , Ant Design, Vue2.x Mysql ...

  9. secure 审计暴力登陆

    文件路径 cd /var/log -rw------- 1 root root 1200063 Aug 10 20:04 secure 做应急响应,或者做脚本监控的时候,都可以参考如下特征 ... A ...

  10. 使用ASP.NET Blazor Server 写混合桌面程序的疯狂想法

    开发本地桌面程序,使用进程内浏览器+进程内BLAZOR服务器,然后任性写功能,自由分发,放飞自我,大家看怎么样? 求评估,求批评 https://github.com/congzhangzh/desk ...