CS/Algorithm

[Java] 백준 2852번: NBA 농구

코드스피드 2025. 6. 28. 17:28
반응형

https://www.acmicpc.net/problem/2852

 

 

 

 

[풀이]

미리 득점에 대한 정보를 data 변수에 넣어놨다.

그리고 0초부터 2880초(48분)이 되기 전까지 1초씩 반복하면서 득점 상황을 보며 비교한다.

 

다만, data에 들어오는 시간이 시간 순으로 기록되어 있는 것이 보장되어 있기 때문에, 

모든 data를 다 확인하는 것이 아니라, lastIndex값을 기록하여 해당 시간이 지났다면, 다음 index를 조회할 수 있도록 값을 저장시켜두었다.

 

추후, 시간조건이 매우 커진다면 득점 상황만을 가지고 더 효율적인 코드를 만들어야 할 것이다.

 

 

 

[코드]

import java.io.*;
import java.util.*;

public class Main {
    public static int ascore = 0;
    public static int bscore = 0;
    public static int atime = 0;
    public static int btime = 0;

    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st;
        
        int n = Integer.parseInt(br.readLine());
        int lastIdx = 0;
        int[][] data = new int[n][2];

        for (int i = 0; i < n; i++) {
            st = new StringTokenizer(br.readLine());
            data[i][0] = Integer.parseInt(st.nextToken());
            data[i][1] = stringToTime(st.nextToken());
        }

        for (int i = 0; i < 2880; i++) {
            if (lastIdx < n && data[lastIdx][1] == i) {
                if (data[lastIdx][0] == 1) ascore++;
                else bscore++;
                lastIdx++;
            }

            if (ascore > bscore) atime++;
            else if (ascore < bscore) btime++;
        }

        System.out.println(timeToString(atime));
        System.out.println(timeToString(btime));
    }

    private static int stringToTime(String time) {
        String[] t = time.split(":");
        return Integer.parseInt(t[0]) * 60 + Integer.parseInt(t[1]);
    }

    private static String timeToString(int time) {
        return (time / 60 < 10 ? "0" : "") + time / 60 + ":" + (time % 60 < 10 ? "0" : "") + time % 60;
    }
}

 

 

 

[시간복잡도]

O(N)

반응형