C. Similar Pairs

We call two numbers xx and ysimilar if they have the same parity (the same remainder when divided by 22), or if |xy|=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 (1t10001≤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 (2n502≤n≤50) — length of array aa.

The second line contains nn positive integers a1,a2,,ana1,a2,…,an (1ai1001≤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.

地址:http://codeforces.com/contest/1360/problem/C

思路:当奇数或偶数的个数为偶数的时候输出yes,否则循环查看是否有一组是相邻的数,若有则yes否则no

int main()
{
    int t ;
    cin >> t;
    while (t--)
    {
        int n;
        cin >> n;
        int a[150] = { 0 };
        int ji=0,ou=0;
        for (int i = 0; i < n; i++)
        {
            int te;
            cin >> te;
            a[te]++;
            if (te % 2 == 0) ou++;
            else ji++;
        }
        if (ji % 2 == 0)
        {
            cout << "YES\n";
        }
        else
        {
            int flag = 0;
            for (int i = 1; i < 100; i++)
            {
                if (a[i] && a[i + 1])
                {
                    flag = 1;
                    break;
                }
            }
            if (flag) cout << "YES\n";
            else cout << "NO\n";
        }
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/yin101/p/12963840.html