문제

https://codeforces.com/problemset/problem/1399/A

걸린 시간

00 : 15 : 44

풀이

C++

#include <bits/stdc++.h>
#define INF 987654321
typedef long long ll;
using namespace std;

int n;
vector<int> a;

void solve(){
    int last = a[0];
    for(int i = 1; i < n; i++){
        if(abs(last-a[i]) <= 1){
            last = a[i];
        }
        else{
            cout << "NO\n";
            return;
        }
    }
    cout << "YES\n";
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    int tc;
    cin >> tc;
    while(tc--){
        cin >> n;
        a.resize(n);
        for(auto& i : a) cin >> i; 
        sort(a.begin(), a.end());
        solve();
    }
    return 0;
}

양의 정수가 저장되어있는 배열 a가 주어진다. 임의로 두 수를 골라 차이의 절대값이 1보다 작거나 같으면 그 중 작은 수를, 같으면 둘 중 아무거나 제거할 수 있을 때 최종적으로 배열에 원소가 하나가 남도록 할 수 있으면 "YES" 를, 그렇지 않으면 "NO" 를 출력하도록 한다.

입력받은 값을 오름차순으로 정렬해서 순서대로 고른 두 값의 차이가 2이상인 원소가 존재하는지를 찾으면 쉽게 해결할 수 있다.

'Codeforces' 카테고리의 다른 글

Codeforces #1409B Minimum Product  (0) 2020.09.18
Codeforces #1399B Gifts Fixing  (0) 2020.09.18
Codeforces #1401A Distance and Axis  (0) 2020.09.16
Codeforces #1418A Buying Torches  (0) 2020.09.16
Codeforces #1406B Maximum Product  (0) 2020.09.14

댓글