[CodeForces Round #784] C. Odd/Even Increments (C++)

2022. 5. 1. 05:48

백준문제풀이 스터디 9주차

CodeForces에서 오랜만에 Div.4 수준 (백준 기준 Gold 1 이하 수준이라고 함) 문제들로 구성된 대화가 개최되었다.

해당 대회 문제를 풀고싶어서 이번 스터디에는 대회 가상 참여 (virtual participation)을 하게 되었다. 혹시 관심 있으면

아래 사이트에서 "start virtual contest"버튼을 눌러 참가해보시죠.

 

간단하게 설명드리자면 채점 방식은 ICPC와 같고, 2시간동안 진행이 된다.

 

https://codeforces.com/contest/1669

 

https://codeforces.com/contest/1669

 

codeforces.com


문제

Given an array a=[a1,a2,…,an]a=[a1,a2,…,an] of nn positive integers, you can do operations of two types on it:

  1. Add 1 to every element with an odd index. In other words change the array as follows: a1:=a1+1,a3:=a3+1,a5:=a5+1,…a1:=a1+1,a3:=a3+1,a5:=a5+1,….
  2. Add 1 to every element with an even index. In other words change the array as follows: a2:=a2+1,a4:=a4+1,a6:=a6+1,…a2:=a2+1,a4:=a4+1,a6:=a6+1,….

Determine if after any number of operations it is possible to make the final array contain only even numbers or only odd numbers. In other words, determine if you can make all elements of the array have the same parity after any number of operations.

Note that you can do operations of both types any number of times (even none). Operations of different types can be performed a different number of times.

입력

The first line contains an integer t (1≤t≤100) — the number of test cases.

The first line of each test case contains an integer nn (2≤n≤50) — the length of the array.

The second line of each test case contains nn integers a1,a2,…,an (1≤ai≤10^3) — the elements of the array.

Note that after the performed operations the elements in the array can become greater than 10^3.

출력

Output t lines, each of which contains the answer to the corresponding test case. As an answer, output "YES" if after any number of operations it is possible to make the final array contain only even numbers or only odd numbers, and "NO" otherwise.

You can output the answer in any case (for example, the strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive answer).


풀이 방식

1. 테스트케이스 갯수인 t 입력받기

2. 테스트케이스의 array 길이 입력

3. array 입력받기

4. 첫 번째 / 두 번째 숫자의 홀짝 여부 확인

5. 홀짝 여부 같은지 확인

- 첫 번째(홀/짝) = 홀수 번째(홀/짝)

- 두 번째(홀/짝) = 짝수 번째(홀/짝)


코드

#include <iostream>
using namespace std;

int main()
{
    int t;      // the number of testcases
    int n;      // the length of the array
    cin >> t;

    int x;          // (1≤x≤103)
    int flag;
    int first, second;
    for (int i = 0; i < t; i++) {
        flag = 0;
        cin >> n;

        cin >> first;
        cin >> second;

        if (first % 2 == 0) {
            first = 0;
        } else {
            first = 1;
        }
        if (second % 2 == 0) {
            second = 0;
        } else {
            second = 1;
        }
        
        // 첫번째, 두번째 숫자 확인
        for (int j = 3; j < n+1; j++) {
            cin >> x;
            
            if (j % 2 == 1) {
                if (x % 2 != first) {
                    flag = 1;
                }
            } else {
                if (x % 2 != second) {
                    flag = 1;
                }
            }
        }

        if (flag == 0) {
            cout << "YES" << endl;
        } else {
            cout << "NO" << endl;
        }
    }

    return 0;
}

https://codeforces.com/contest/1669/problem/C

 

https://codeforces.com/contest/1669/problem/C

 

codeforces.com

 

BELATED ARTICLES

more