Codeforces 1360C - Similar Pairs

2020-05-28  本文已影响0人  费城的二鹏

水果披萨味道还不错,就是烤的时间稍微长了点,还是香蕉简单。

翻译

相似对

如果两个数字 x 和 y 同是奇数、同是偶数或者相差1。

给你一个长度为 n 的数组 a,n 是偶数,检查这个数组是否能分成 n/2 对相似对。

输入格式

输入整数 t,表示测试用例组数。

每个测试用例输入两行,第一行,输入一个偶数 n,第二行,输入 n 个数字。

输出格式

每个测试用例,如果可以全都构成相似对,输出 YES。否则输出,NO。

分析

贪心算法题,如果奇数的数量是偶数个,那么一定能全部组成相似对。如果不是,那么需要有两个数字连着,如果没有连着的,那么可以构成。

代码(PyPy3)

# https://codeforces.com/problemset/problem/1360/C

import sys
import os
import heapq

try:
    path = "./file/input.txt"
    if os.path.exists(path):
        sys.stdin = open(path, 'r')
    # sys.stdout = open(r"./file/output.txt", 'w')
except:
    pass

t = int(input())

def printd(value):
    # print(value)
    pass

for _ in range(t):
    n = int(input())
    arr = list(map(int, input().split(" ")))
    arr.sort()
    
    isYes = False
    even = 0
    odd = 0
    for i in range(0, n):
        if i > 0:
            if arr[i] - arr[i - 1] == 1:
                isYes = True
        if arr[i] % 2 == 0:
            even += 1

    if even % 2 == 0:
        isYes = True        

    print("YES" if isYes else "NO")

更多代码尽在 https://github.com/Tconan99/Codeforces

by 费城的二鹏 2020.05.25 长春

上一篇下一篇

猜你喜欢

热点阅读