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;
    }
    
  • 相关阅读:
    在Linux中常用的启动引导工具:grub和lilo
    Linux的启动流程
    时间同步
    LINUX时区的设置
    Linux时间设置命令
    Linux时间介绍
    mysql特殊处理
    PHP 设计模式 笔记与总结(8)策略模式
    Java实现 LeetCode 172 阶乘后的零
    Java实现 LeetCode 172 阶乘后的零
  • 原文地址:https://www.cnblogs.com/Hayasaka/p/14294158.html
Copyright © 2011-2022 走看看