zoukankan      html  css  js  c++  java
  • Codeforces Round #661 (Div. 3) A. Remove Smallest

    You are given the array aa 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 (a_i)and (a_j)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 aa a . The second line of the test case contains n integers (a_1,a_2...a_n) (1≤(a_i)≤100), where (a_i) 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

    Copy

    5
    3
    1 2 2
    4
    5 5 5 5
    3
    1 2 4
    4
    1 3 4 4
    1
    100
    

    Output

    Copy

    YES
    YES
    NO
    NO
    YES
    

    水,排序后扫一遍判断相邻的数是否相差小于等于1即可。

    #include <bits/stdc++.h>
    using namespace std;
    int a[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);
    		bool flag = 1;
    		for(int i = 2; i <= n; i++)
    		{
    			if(a[i] > a[i - 1] + 1)
    			{
    				cout << "NO" << endl;
    				flag = 0;
    				break;
    			}
    		} 
    		if(flag) cout << "YES" << endl;
    	}
    	
    	return 0;
    }
    
  • 相关阅读:
    Row versus Set Processing, Surprise!(集合处理和单行处理数据的差异性)
    SVN导出增量包的方法
    Number of Digit One
    VPS+LNMP+WordPress搭建个人网站
    寻找最小的k个数(大顶堆方法)
    二叉搜索树转化为有序双向链表(C语言)
    LeetCode 345 Reverse Vowels of a String
    LeetCode 278 First Bad Version
    LeetCode 205 Isomorphic Strings
    LeetCode 88 Merge Sorted Array
  • 原文地址:https://www.cnblogs.com/lipoicyclic/p/13452635.html
Copyright © 2011-2022 走看看