둘이서 번갈아가며 나무를 두 조각으로 자르는데, 자기 턴에 나무를 못 자르면 집니다.
나무는 자연수로만 자를 수 있고, 길이가 1이 되면 자르지 못합니다.
누가 나무 자르기 게임에서 이기는지 출력하는 문제입니다.
나무의 길이 - 1 횟수만큼 자를 수 있으니, 길이 - 1을 모두 더합니다.
그 값이 홀수면 첫 번째로 자르는 애가, 짝수면 두 번째로 자르는 애가 이깁니다.
#include <stdio.h>
int main() {
int t;
scanf("%d", &t);
for(int p = 0; p < t; p++) {
int n, temp, sum = 0;
scanf("%d", &n);
for(int i = 0; i < n; i++) {
scanf("%d", &temp);
sum += temp - 1;
}
if(sum % 2 == 1) printf("errorgorn\n");
else printf("maomao90\n");
}
}
B. I love AAAB
https://codeforces.com/contest/1672/problem/B
A(개수 무제한)B(무조건 한 개)의 문자열을 순서 상관없이 입력해서 input으로 준 문자열을 만들 수 있는지 체크하는 문제입니다.
예를 들어 AAABBAAAB를 만들어봅시다.
빨간색이 추가해준 문자열입니다.
AAAAB -> AABAAAB -> AAABBAAAB == AAABBAAAB.
이런 식으로 막무가내로 끼워넣기도 가능합니다.
1. 문자열 시작은 A, 끝은 B
2. 여태 나온 A 개수보다 B 개수가 더 많으면 안 된다. ex> AABBBAAAB (5번째에서 A 둘, B 셋으로 안 됨)
충족하면 YES 아님 NO
#include <stdio.h>
#include <string.h>
int main() {
int t;
char s[200001] = {};
scanf("%d", &t);
for(int p = 0; p < t; p++) {
int n, temp = 0, sum = 0, no = 0;
scanf("%s", s);
for(int i = 0; i < strlen(s); i++) {
if(s[i] == 'B') sum++;
if(s[i] == 'A') temp++;
if(sum > temp) no = 1;
}
if(s[strlen(s) - 1] == 'A') no = 1;
if(no) printf("NO\n");
else printf("YES\n");
}
}
C. Unequal Array
https://codeforces.com/contest/1672/problem/C
You are given an array 𝑎a of length 𝑛n. We define the equality of the array as the number of indices 1≤𝑖≤𝑛−11≤i≤n−1 such that 𝑎𝑖=𝑎𝑖+1ai=ai+1. We are allowed to do the following operation:
- Select two integers 𝑖i and 𝑥x such that 1≤𝑖≤𝑛−11≤i≤n−1 and 1≤𝑥≤1091≤x≤109. Then, set 𝑎𝑖ai and 𝑎𝑖+1ai+1 to be equal to 𝑥x.
Find the minimum number of operations needed such that the equality of the array is less than or equal to 11.
Each test contains multiple test cases. The first line contains a single integer 𝑡t (1≤𝑡≤1041≤t≤104) — the number of test cases. The description of the test cases follows.
The first line of each test case contains an integer 𝑛n (2≤𝑛≤2⋅1052≤n≤2⋅105) — the length of array 𝑎a.
The second line of each test case contains 𝑛n integers 𝑎1,𝑎2,…,𝑎𝑛a1,a2,…,an (1≤𝑎𝑖≤1091≤ai≤109) — elements of the array.
It is guaranteed that the sum of 𝑛n over all test cases does not exceed 2⋅1052⋅105
For each test case, print the minimum number of operations needed.
배열에서 arr[i] == arr[i + 1] 이 되는 경우가 최대 1회가 되도록 하는 문제입니다.
임의로 x를 골라서 arr[x]와 arr[x + 1]을 내가 원하는 값으로 바꿔주는 작업을 해줄 수 있습니다.
위의 작업을 하는 최소의 경우를 찾으면 됩니다.
예를 들어
1 1 2 5 4 1 1
이런 배열을 받은 경우 결국 양 끝의 2개가 있는 지점에서부터 가운데로 좁혀와서 중복되는 숫자를 없애줘야 최솟값이 됩니다.
1 1 2 5 7 7 1 -> 1 9 9 5 7 7 1 -> 1 9 8 8 7 7 1 -> 1 9 8 3 3 7 1
양 끝에서 연속되는게 언제 등장하는지 구해서 그 사이만큼 저렇게 변화시키는 값을 구해주면 됩니다.
#include <stdio.h>
int main() {
int t;
int arr[200001] = {};
scanf("%d", &t);
for(int i = 0; i <t; i++) {
int n, temp = 0;
scanf("%d", &n);
for(int j = 0; j < n; j++) {
scanf("%d", &arr[j]);
}
int count = 1, cc = 0;
int startpoint = 0, end = 0;
for(int j = 1; j < n; j++) {
if(arr[j] == arr[j - 1]) {
startpoint = j;
break;
}
}
for(int j = n - 1; j > 0; j--) {
if(arr[j] == arr[j - 1]) {
end = j;
break;
}
}
if(end - startpoint < 1) cc = 0;
else if(end - startpoint <= 2) cc= 1;
else cc = end - startpoint -1;
printf("%d\n", cc);
}
}
'코드포스' 카테고리의 다른 글
코드포스 Educational Codeforces Round 129 A, B, C 풀이 (0) | 2022.05.24 |
---|---|
코드포스 #793 (Div. 2) (0) | 2022.05.23 |
코드포스 #792 (Div. 1 + Div. 2) A, B 풀이 (0) | 2022.05.20 |
코드포스 #779 (Div. 2) - A, C 풀이 (0) | 2022.05.19 |
코드포스 Educational Codeforces Round 72 (Rated for Div. 2) - A 풀이 (0) | 2022.05.19 |