zoukankan      html  css  js  c++  java
  • Codeforces 1399A

    A. Remove Smallest

    time limit per test1 second
    memory limit per test256 megabytes
    input standard input
    output standard output
    You are given the array 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 ai and aj 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 a. The second line of the test case contains n integers a1,a2,…,an (1≤ai≤100), where ai 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
    5
    3
    1 2 2
    4
    5 5 5 5
    3
    1 2 4
    4
    1 3 4 4
    1
    100
    output
    YES
    YES
    NO
    NO
    YES

    Note

    In the first test case of the example, we can perform the following sequence of moves:

    choose i=1 and j=3 and remove ai (so a becomes [2;2]);
    choose i=1 and j=2 and remove aj (so a becomes [2]).
    In the second test case of the example, we can choose any possible i and j any move and it doesn’t matter which element we remove.

    In the third test case of the example, there is no way to get rid of 2 and 4.

    题目大意:

    给出 t 组样例,每组样例输入一个 n ,第二行输入n 个数,你可以选择其中任意两个差 < = 1的数并消除这两个比较小的数(相等则消除一个),询问最后是不是能只剩下一个元素。

    解题思路:

    排序即可,1 2 3 4 5 可以 1 2 消除1 推过去最后剩下5,如果有任意一对相邻的数差 >1 则不行。

    Code:

    #pragma GCC optimize(2)
    #include <cstdio>
    #include <iostream>
    #include <algorithm>
    #include <cmath>
    #include <vector>
    #include <queue>
    #include <stack>
    #include <map>
    #include <set>
    #include <cstring>
    using namespace std;
    typedef long long ll;
    typedef pair<int, int> pii;
    const int N = 500;
    int a[N];
    int main()
    {
    	ios::sync_with_stdio(false);
    	int t;
    	cin >> t;
    	while (t--)
    	{
    		int n;
    		cin >> n;
    		for (int i = 1; i <= n; i ++)
    			cin >> a[i];
    		sort(a + 1, a + 1 + n);
    		int cnt = 0;
    		for (int i = 1; i < n; i ++)
    		{
    			if (abs(a[i] - a[i + 1]) > 1)
    				cnt ++;
    		}
    		if (cnt < 1)
    			cout << "YES";
    		else
    			cout << "NO";
    		cout << endl;
    	}
    	return 0;
    }
    
  • 相关阅读:
    洛谷 P1202 [USACO1.1]黑色星期五Friday the Thirteenth 题解
    洛谷 P1957 口算练习题 题解
    洛谷 P2036 Perket 题解
    洛谷 P2369 EXCEEDED WARNING A 题解
    洛谷 P1184高手之在一起 题解
    洛谷 P1897电梯里的爱情 题解
    Python做的眼睛护士
    Python-Tkinter的Entry详解
    Python的TkinterButton做为父窗口
    Python用Tkinter的Frame实现眼睛护士的倒计时黑色屏幕
  • 原文地址:https://www.cnblogs.com/Hayasaka/p/14294158.html
Copyright © 2011-2022 走看看