알고리즘/BOJ

백준 2852번 NBA 농구

꾸준함. 2018. 9. 24. 20:12

문제 링크입니다: https://www.acmicpc.net/problem/2852

 

생각보다 까다로운 문제였습니다.

 

알고리즘은 아래와 같습니다.

1. 한 팀이 이기기 시작하는 시간을 start에 저장합니다.

2. 비기는 순간 해당 팀이 이기고 있었던 시간을 계산해서 더해줍니다.

3. 마지막에 한 팀이 이기고 있었을 경우 48분에서 start를 뺀 시간만큼 이기고 있었던 팀에 더해줍니다.

4. string 형식으로 출력해야지만 AC를 맞는 것 같습니다.(숫자를 integer 형식으로 제출하면 WA가 뜨는데 이유가 궁금하네요)

 

주의할 점은, 초의 범위는 0 이상 59 이하라는 것입니다. 

해당 범위를 초과할 경우 1분을 더해주고 60초를 빼주고, 해당 범위보다 미만이라면 1분을 빼주고 60초를 더해줘야합니다.

 

#include <iostream>

#include <vector>

#include <string>

#include <algorithm>

using namespace std;

 

int main(void)

{

        ios_base::sync_with_stdio(0);

        cin.tie(0);

        int N;

        cin >> N;

 

        int team[3];

        pair<int, int> start[3]; //이기기 시작한 시간

        pair<int, int> result[3];

        bool reset = true;

        for (int i = 0; i < N; i++)

        {

                 pair<int, string> temp;

                 cin >> temp.first >> temp.second;

 

                 team[temp.first]++;

                 if (team[1] == team[2])

                 {

                         int minute = (temp.second[0] - '0') * 10 + temp.second[1] - '0' - start[3 - temp.first].first;

                         int second = (temp.second[3] - '0') * 10 + temp.second[4] - '0' - start[3 - temp.first].second;

                         if (second < 0)

                         {

                                 second += 60;

                                 minute -= 1;

                         }

 

                         result[3 - temp.first].first += minute;

                         result[3 - temp.first].second += second;

                        

                         if (result[3 - temp.first].second > 60)

                         {

                                 result[3 - temp.first].first += 1;

                                 result[3 - temp.first].second -= 60;

                         }

                         reset = true;

                 }

 

                 if (team[1] != team[2] && reset)

                 {

                         start[temp.first].first = (temp.second[0] - '0') * 10 + temp.second[1] - '0';

                         start[temp.first].second = (temp.second[3] - '0') * 10 + temp.second[4] - '0';

                         reset = false;

                 }

        }

 

        for(int i=1; i<=2; i++)

                 if (team[i] > team[3 - i] && (start[i].first || start[i].second))

                 {

                         int minute = 48 - start[i].first;

                         int second = -start[i].second;

                         if (second < 0)

                         {

                                 minute--;

                                 second += 60;

                         }

 

                         result[i].first += minute;

                         result[i].second += second;

 

                         if (result[i].second > 60)

                         {

                                 result[i].first++;

                                 result[i].second -= 60;

                         }

                 }

        for (int i = 1; i <= 2; i++)

        {

                 string s;

                 if (0 <= result[i].first && result[i].first <= 9)

                         s += '0';

                

                 string a;

                 if (result[i].first)

                 {

                         while (result[i].first)

                         {

                                 a += (result[i].first % 10) + '0';

                                 result[i].first /= 10;

                         }

                         reverse(a.begin(), a.end());

                         s += a;

                         a.clear();

                 }

                 else

                         s += '0';

                 s += ":";

                 if (0 <= result[i].second && result[i].second <= 9)

                         s += '0';

                 if (result[i].second)

                 {

                         while (result[i].second)

                         {

                                 a += (result[i].second % 10) + '0';

                                 result[i].second /= 10;

                         }

                         reverse(a.begin(), a.end());

                         s += a;

                         a.clear();

                 }

                 else

                         s += '0';

                 cout << s << "\n";

        }

        return 0;

}

 

개발환경:Visual Studio 2017

지적, 조언, 질문 환영입니다! 댓글 남겨주세요~

 

2024.03.14 업데이트된 코드

 

반응형

'알고리즘 > BOJ' 카테고리의 다른 글

백준 2243번 사탕상자  (0) 2018.09.25
백준 2023번 신기한 소수  (2) 2018.09.24
백준 10809번 알파벳 찾기  (0) 2018.09.24
백준 5624번 좋은 수  (0) 2018.09.24
백준 1713번 후보 추천하기  (0) 2018.09.24