Loading [MathJax]/jax/output/CommonHTML/jax.js

Codeforces

Codeforces Global #18

yujaa 2021. 12. 26. 19:47

A. Closing The Gap

Problem - A - Codeforces

 

Problem - A - Codeforces

 

codeforces.com

타워 n개가 주어진다.

타워의 최대 높이 - 최소 높이의 값을 최소화하는 문제이다.

 

타워들의 높이 합이 n으로 나누어 떨어지면 0, 나누어 떨어지지 않으면 1이다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<intint> pii;
 
int main(void) {
    int t; cin >> t;
    while (t--) {
        int n; cin >> n;
        int sum = 0;
        for (int i = 0; i < n; i++) {
            int tmp; cin >> tmp;
            sum += tmp;
        }
        cout << (sum % n ? 1 : 0<< '\n';
    }
    return 0;
}
cs

 

 

B. And It's Non-Zero

Problem - B - Codeforces

 

Problem - B - Codeforces

 

codeforces.com

[l,r] 범위에서 숫자를 제거하여 bitwise AND 값이 0이 아니도록 만들 때, 최소 제거 횟수를 구하는 문제이다.

 

psum[i][j][1,j] 범위에서 i번째 비트가 0인 숫자들의 개수로 정의하자.

답은 min(psum[i][r]psum[i][l1])이다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<intint> pii;
 
const int MAX = 2e5+2;
int p[20][MAX];
 
int main(void) {
    for (int i = 0; i < 18; i++) {
        int cur = (1 << i);
        for (int j = 1; j < MAX; j++) {
            p[i][j] = p[i][j-1];
            if ((j & cur) == 0) {
                p[i][j]++;
            }
        }
    }
    int t; cin >> t;
    while (t--) {
        int l, r;
        cin >> l >> r;
        int mn = INT_MAX;
        for (int i = 0; i < 18; i++) {
            mn = min(mn, p[i][r] - p[i][l-1]);
        }
        cout << mn << '\n';
    }
    return 0;
}
cs

 

 

C. Menorah

Problem - C - Codeforces

 

Problem - C - Codeforces

 

codeforces.com

길이 n의 binary string a,b가 주어진다.

어떤 비트 1을 선택하고 move하면 해당 비트를 제외한 나머지 비트들이 flip된다.

a를 b로 만드는 데 최소 몇 번의 move가 필요한지 구하는 문제이다.

 

두 번의 move가 어떤 효과를 갖는지 살펴보자.

1) 같은 비트를 선택했을 때

해당 비트를 제외한 나머지 비트들이 두 번 flip되므로 아무런 효과가 없다.

2) 서로 다른 비트를 선택했을 때

문자열에서 01을 한 번 swap한 것과 같다.

 

다음과 같이 경우를 나누어 답을 구할 수 있다.

a와 b의 i번째 비트를 각각 ai,bi라고 하자.

1) 답이 짝수인 경우

a1의 개수b1의 개수가 동일하면 YES이다.

aibi를 만족하는 i의 개수가 답이다.

2) 답이 홀수인 경우

처음 한 번의 move 이후에는 답이 짝수인 경우와 동일하다.

ai=bi=1i가 존재하고, a 1의 개수 - 1 b 0의 개수가 동일하면 YES이다.

ai=bi를 만족하는 i의 개수가 답이다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<intint> pii;
 
int main(void) {
    int t; cin >> t;
    while (t--) {
        int n; cin >> n;
        string a, b;
        cin >> a >> b;
 
        int cnt = 0;
        int one[2= { 00 };
        bool flag = false;
        for (int i = 0; i < n; i++) {
            if (a[i] == '1') one[0]++;
            if (b[i] == '1') one[1]++;
            if (a[i] == b[i]) {
                if (a[i] == '1') {
                    flag = true;
                }
                cnt++;
            }
        }
        int ans = INT_MAX;
        if (one[0== one[1]) {
            ans = min(ans, n - cnt);
        }
        if (flag && one[0- 1 == n - one[1]) {
            ans = min(ans, cnt);
        }
        cout << (ans == INT_MAX ? -1 : ans) << '\n';
    }
    return 0;
}
cs

 

 

D. X(or)-mas Tree

Problem - D - Codeforces

 

Problem - D - Codeforces

 

codeforces.com

 

 

'Codeforces' 카테고리의 다른 글

Codeforces #763  (0) 2022.01.02
Codeforces Educational #120  (0) 2022.01.02
Codeforces Educational #82  (0) 2021.12.26
Codeforces #619  (0) 2021.12.23
Codeforces #728  (0) 2021.07.17