[BOJ] 11976. Promotion Counting

이정진·2022년 2월 2일
0

PS

목록 보기
39/76
post-thumbnail

Promotion Counting

알고리즘 구분 : 수학, 사칙연산

문제

Bessie the cow is helping Farmer John run the USA Cow Olympiad (USACO), an on-line contest where participants answer challenging questions to demonstrate their mastery of bovine trivia.

In response to a wider range of participant backgrounds, Farmer John recently expanded the contest to include four divisions of difficulty: bronze, silver, gold, and platinum. All new participants start in the bronze division, and any time they score perfectly on a contest they are promoted to the next-higher division. It is even possible for a participant to be promoted several times within the same contest. Farmer John keeps track of a list of all contest participants and their current divisions, so that he can start everyone out at the right level any time he holds a contest.

When publishing the results from his most recent contest, Farmer John wants to include information on the number of participants who were promoted from bronze to silver, from silver to gold, and from gold to platinum. However, he neglected to count promotions as they occurred during the contest. Bessie, being the clever bovine she is, realizes however that Farmer John can deduce the number of promotions that occurred solely from the number of participants at each level before and after the contest. Please help her perform this computation!

입력
Input consists of four lines, each containing two integers in the range 0..1,000,000. The first line specifies the number of bronze participants registered before and after the contest. The second line specifies the number of silver participants before and after the contest. The third line specifies the number of gold participants before and after the contest. The last line specifies the number of platinum participants before and after the contest.

출력
Please output three lines, each containing a single integer. The first line should contain the number of participants who were promoted from bronze to silver. The second line should contain the number of participants who were promoted from silver to gold. The last line should contain the number of participants who were promoted from gold to platinum.

예제 입력 1
1 2
1 1
1 1
1 2
예제 출력 1
1
1
1

문제 풀이


이 문제는 Contest 시작 전 각 레벨 별 참가 인원 수가 정해져 있으며, Contest 종료 후 각 레벨 별 남아있는 인원 수가 주어진다. 이 때, 각 레벨(Bronze, Silver, Gold, Platinum)에서 Promotion 즉, 승급하는 참가 인원 수를 찾아서 구하는 것이 핵심이다. 조심해야할 점은 처음 인원 외에 Contest 중간에 새로운 참가자가 참여할 수 있다는 것인데, 이 참가자들은 모두 Bronze 레벨에서 시작하게 된다. 이러한 문제 상황에서 나는 Contest 중간에 참가하는 인원이 있는지를 파악하였고, 있다면 이를 Bronze레벨에서 시작하는 참가 인원으로 간주하여 추가 시킨 뒤, Contest를 시작하도록 문제를 구현하였다.

소스 코드

#include <bits/stdc++.h>

using namespace std;

int before, after;
int result[3];
int participants[4][2];
void solve();

int main() {
    ios::sync_with_stdio(false);
    cin.tie(NULL);

    before = 0;
    after = 0;
    for(int i = 0; i < 4; i++) {
        cin >> participants[i][0] >> participants[i][1];
        before += participants[i][0];
        after += participants[i][1];
    }

    solve();

    return 0;
}

void solve() {
    int newParticipants = after - before;

    // 새로운 참가자가 있을 경우
    if(newParticipants) {
        participants[0][0] += newParticipants;
    }

    // promote count
    for(int i = 0; i < 4; i++) {
        result[i] = participants[i][0] - participants[i][1];
        participants[i + 1][0] += result[i];
    }

    // 결과 출력
    for(int i = 0; i < 3; i++) {
        cout << result[i] << endl;
    }
}

0개의 댓글