아 진짜 아쉽네요 E에서 뭐가 틀린 건지 한참 헤멨는데 int를 long long으로 바꾸니까 바로 AC 받았습니다.

그것만 신경 써줬으면 1350 정도 퍼포 나왔을텐데 진짜 아쉽네요..

 

세네판 정도 안에 그린 찍고 싶었는데 ㅋㅋ 안 될 것 같네요

 

A. Print a Pedestal (Codeforces logo?)

가운데가 제일 길쭉하고 왼쪽이 그 다음, 오른쪽이 그다음으로 길도록 해주는 단순 구현 문제입니다.

#include <stdio.h>

int main() {
int n, t;
scanf("%d", &t);
while(t--) {
    scanf("%d", &n);
    int first = n / 3 + 1;
    if(n % 3 != 0) first++;
    int second = n - first;
    second /= 2;
    second++;
    int third = n - first - second;
    printf("%d %d %d\n", second, first, third);
}
}

 

B. Array Decrements

이번 코포는 B 때문에 망했다고 해도 과언이 아닌..

#include <stdio.h>

int main() {
int n,m, t;
int arr[1000000] = {};
int brr[1000000] = {};
scanf("%d", &t);
while(t--) {
    scanf("%d", &n);
    int temp = 0,NO = 0,no = 0;
    for(int i = 0; i < n; i++) {
        scanf("%d", &arr[i]);
    }
    for(int i = 0; i < n; i++) {
        scanf("%d", &brr[i]);
        if(arr[i] < brr[i]) NO = 1;
    }
    for(int i = 0; i < n; i++) {
            if(temp < arr[i] - brr[i]) temp = arr[i] - brr[i];
    }
    if(temp == 0) temp = arr[0] - brr[0];
    for(int i = 0; i < n; i++) {
        arr[i] -= temp;
    }
    for(int i = 0; i < n; i++) {
        if(arr[i] != brr[i]) {
            no++;
            if(brr[i] == 0 && arr[i] <= 0) no--;
        }
    }
    if(no || NO) printf("NO\n");
    else printf("YES\n");
}
}

이렇게 어렵게 푸는 것도 맞는지도 모르겠습니다 사실..

여태 코포 몇판 안 해봤지만 중간에 문제 하나 막히면 거기서 말리기 시작하는 타입인 거 같아요 저는

 

C. Restoring the Duration of Tasks
 

일 끝나기 전에 다음 task 주면, task 끝낸 시간의 간격 출력.

일 끝나고 난 후에 다음 task 주면 task 준 시간과 끝낸 시간의 간격 출력.

푸는 데에 5분도 안 걸렸습니다.

#include <stdio.h>
int main() {
int n, t;
long long arr[300000] = {};
long long brr[300000] = {};
scanf("%d", &t);
while(t--) {
    scanf("%d", &n);
    for(int i = 0; i < n; i++) {scanf("%lld", &arr[i]);
    }
    for(int i = 0; i < n; i++) {scanf("%lld", &brr[i]);
    }
    printf("%lld ", brr[0] - arr[0]);
    for(int i = 1; i < n; i++) {
        if(arr[i] < brr[i - 1]) printf("%lld ", brr[i] - brr[i - 1]);
        else printf("%lld ", brr[i] - arr[i]);
    }
    printf("\n");
}
}

D. Black and White Stripe

 

문자열에서 0~k-1 의 문자열 안에 W가 얼마나 들어있는지 찾고

앞으로 한 칸씩 나가면서 가장 적은 값 출력.

엄청 쉬운 문제인데 구현 실수해서 2번이나 틀렸습니다.

근데 이건 제 실력이 부족한 탓이에요

#include <stdio.h>
#include <algorithm>

int main() {
int n,m, t;
char s[300000] = {};
scanf("%d", &t);
while(t--) {
 scanf("%d", &n);
 scanf("%d", &m);
 scanf("%s", s);
 int count = 0, min = 10000000;
 for(int i = 0; i < m; i++) {
     if(s[i] == 'W') count++;
 }
 if (min > count) min = count;
 for(int i = 0; i < n - m; i++) {
     if(s[m + i] == 'W') count++;
     if(s[i] == 'W') count--;
     if(min > count) min = count;
 }
 printf("%d\n", min);
}
}

E. Price Maximization

 

맨 처음에 각각 물건 입력받으면서 k로 나눈 값 더해주고,

나머지는 모아서 배열에 담은 다음에 작은놈들끼리 짝지어주면서 짝 맞으면 더해주는 식으로 짰습니다...

 

구현까지도 괜찮았는데 long long을 쓰는 걸 깜빡했네요

𝑎𝑛은 10억까지도 가능하니까 당연히 long long인데 이걸 뭔 생각으로 int로 출력하려 했는지..  앞으로 

#define int long long 쓰겠습니다 짜증나서..

#include <stdio.h>

int main() {
int n,m, t;
int k[1001] = {};
scanf("%d", &t);
while(t--) {
    scanf("%d%d", &n, &m);
    long long temp = 0;
    long long ans = 0;
    for(int i = 0; i < n; i++) {
        scanf("%lld", &temp);
        ans += temp / m;
        k[temp % m]++;
    }

    int end = m, first = 1;
    
    for(int i = 0; i <= m; i++) {
        for(int j = m - i; j <= m; j++) {
                if(i == j) {
                    ans+= k[i] / 2;
                    k[i] %= 2;
                    continue;
                }
                if(k[i] > 0 && k[j] > 0) {
                if(k[i] > k[j]) {
                    int temp = k[j];
                        ans += k[j];
                    k[i] -= temp;
                    k[j] -= temp;
                }
                else {
                    int temp = k[i];
                    ans += k[i];
                    k[i] -= temp;
                    k[j] -= temp;
                }
                                    //printf("%d %d\n", k[i], k[j]);
            }
        }
    }
    for(int i = 0; i < 1000; i++) {
        k[i] = 0;
    }
    printf("%lld\n" , ans);
}
}

 

https://codeforces.com/contest/1681/problem/A

 

Problem - A - Codeforces

 

codeforces.com

3솔 했습니다.

 

A. Game with Cards

 

각자 카드를 번갈아서 내는데, 방금 상대방이 낸 것보다 숫자가 큰 카드를 내야합니다.

Alice가 먼저 카드를 내면 누가 이기는지, Bob이 먼저 카드를 내면 누가 이기는지 출력해주면 됩니다.

 

각자 제일 큰 카드의 값이 같다면, 먼저 내는 애가 이기고(더 큰 카드를 낼 수 없으니까)

아니라면 상대방보다 더 큰 카드를 갖고 있는 애가 무조건 이깁니다.

 

 

B. Card Trick

 

카드 뭉치 위에서 b장을 뽑아서 밑으로 내리는 작업을 m번 했을 때, 가장 위에 오는 카드의 번호를 출력하는 문제입니다.

배열로 카드 원소를 각각 입력받고, 밑으로 내리는 카드 b장을 다 더해서 카드의 총 장수로 나머지 연산을 하면 됩니다.

 ex) [1,2,3,4,5] 5장이라고 가정, 위에서부터 2, 3, 2개 내리는 작업을 했다면 7 % 5 => 2, arr[2] = 3 이런 식입니다.

#include <stdio.h>

int main() {
    int t;
    scanf("%d", &t);
    int arr[300000] = {}, brr[300000] = {};
    while(t--) {
        int n, m;
        int alice = 0, bob = 0;
        scanf("%d", &n);
        for(int i = 0; i < n; i++) {
            scanf("%d", &arr[i]);
        }
        scanf("%d", &m);
        int temp, sum = 0;
        for(int i = 0; i< m ; i++) {
            scanf("%d", &temp);
            sum += temp;
            while(sum >= n) {
                sum -= n;
            }
        }
        printf("%d\n", arr[sum]);
    }
}

 

C. Double Sort

 

이거는 2행짜리 배열에서 i열, j열을 골라서 값을 바꿀 때,

각 행이 non-decreasing 하게 정렬이 될 수 있는지,

된다면 몇번 바꿔야 하는지, 그리고 그 바꿀 때의 i와 j를 출력해줘야 하고,

못 바꾸거나 바꿔야하는 횟수가 1만회 넘어가면 -1 출력.

 

이건 그냥 구현입니다.

이중 for문에서

arr[i] > arr[j] && brr[i] < brr[j] 인 경우나

arr[i] < arr[j] && brr[i] > brr[j] 인 경우가 한 개라도 있으면 -1 출력.

아니라면 arr기준으로 먼저 정렬해주고 그 다음에 brr 기준으로 정렬 해줬습니다.

값이 중복될 경우 정렬이 안 될 수 있기에 나눠서 두번 해줬습니다.

ex)

arr [1,1,1,1,1]

brr[5,4,3,2,1]  인 경우 arr만 정렬하거나 혹은 거꾸로인데 brr만 정렬하면 정렬이 안되니까... 

 

A. Log Chopping

둘이서 번갈아가며 나무를 두 조각으로 자르는데, 자기 턴에 나무를 못 자르면 집니다.

나무는 자연수로만 자를 수 있고, 길이가 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

 

Problem - B - Codeforces

 

codeforces.com

 

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

 

Problem - C - Codeforces

 

codeforces.com

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.

Input

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𝑛21052≤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 21052⋅105

Output

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);
    }
}

 

너무 긴장했나?
그냥 망쳤습니다. 평소 div2 풀어보던 실력의 절반도 못했어요.
부끄러우니 제가 몇분만에 풀었는지는 공개 안 하겠습니다.
https://codeforces.com/contest/1684

Dashboard - Codeforces Round #792 (Div. 1 + Div. 2) - Codeforces

codeforces.com

Alice가 먼저 숫자 2개 순서를 바꾸고, Bob이 맨뒤에서부터 숫자 하나씩 지웁니다.
Alice는 최대한 작은 숫자가 마지막까지 살아있도록 해야합니다.
2자리인 경우에는 걍 뒤에 있는 숫자가 출력됩니다.
3자리 이상인 경우엔 무조건 최소인 숫자가 출력됩니다.
-----예-------시-------- ( '/' 이게 뒤에서 자른 거라고 생각하세요.)
132 -> 31 / 2 -> 1 / 3
321 -> 31 / 2 -> 1 / 3
312 -> 21 / 3 -> 1 / 2

숫자 길이가 3인 경우에서도 되니까 더 크면 걍 무조건 되겠죠?

#include <stdio.h>
#include <string.h>
int main() {
    int t, n;
    char s[11] = {};
    scanf("%d", &t);
    for(int i = 0; i < t; i++) {
        int min = 10,miin = 10, count = 0;
        scanf("%s", s);
        for(int j = 0; j < strlen(s); j++) {
            if(s[j] - 48 < min) {
                min = s[j] - 48;
                count = j;
            }
        }
        s[count] = '0';
        for(int j = 0; j < strlen(s); j++) {
            if(s[j] - 48 < miin && s[j] != '0') {
                miin = s[j] - 48;
            }
        }
        if(strlen(s) == 1) {
            printf("%d\n", min);
        }
        else if(strlen(s) == 2 && count == 0) {
            printf("%d\n", miin);
        }
        else if(strlen(s) % 2 != 0 && count == 0) {
            printf("%d\n", min);
        }
        else printf("%d\n", min);
        for(int k = 0; k < 11; k++) {
            s[k] = 0;
        }
    }
}

아니 A에 코드를 너무 장황하게 짰어요.. 아 짜증나네 A랑 B는 무조건 쉽게 풀린다는 걸 전제하에 두고 문제 풀어야겠습니다.
이게 뭐하자는 건지
다음

a b c 입력해주면

이걸 만족하는 x y z를 구해주면 돼요.

조건에 a < b < c가 있습니다.
1. c가 제일 크니까 z = c 그대로 고정해둡시다.
그리고 y % c 가 b 여야 한대요.
c는 b보다 크죠?
2. y = b 그대로 고정해둡시다.
a만 남았습니다.
x % b = a 이건 만족하는데, c % x = c여야합니다.
그럼 x가 c보단 크면서 b로 나눴을 때 a가 나머지로 나오게 하면 되겠네요.
3. b * c 를 하면 c보다 큰 동시에 b에 나눠떨어집니다. 거기에 + a 하면 끝.

 #include <stdio.h>
int main() {
    int t;
    scanf("%d", &t);
    for(int i = 0; i < t; i++) {
        long long a,b,c;
        scanf("%lld%lld%lld", &a, &b, &c);
        a = a + b * c;
        printf("%lld %lld %lld\n", a, b, c);
    }
}

div 1 + div 2 다신 안 합니다. 1솔이 말인가

+ Recent posts