본문 바로가기
Beakjoon&프로그래머스/Java

[백준/Java] 24610번 Who Goes There?

by 현장 2026. 2. 12.

-Code

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

public class BOJ24610 {
    public static void main(String[] args) throws IOException {
        BufferedReader br =
                new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine());

        int n = Integer.parseInt(st.nextToken());
        int m = Integer.parseInt(st.nextToken());

        int[] teamCnt = new int[m + 1];
        for (int i = 0; i < m; i++) {
            teamCnt[i] = Integer.parseInt(br.readLine());
        }
        int[] answer = new int[m];
        int idx = 0, total = 0;
        while (total < n){
            int teamNextWave = 0;
            for (int i = 0; i < m; i++) {
                if (teamCnt[i] > answer[i]) teamNextWave++;
            }

            if (teamNextWave == 0 || total + teamNextWave > n) {
                break;
            }


            for (int i = 0; i < m; i++) {
                if (teamCnt[i] > answer[i]) {
                    answer[i]++;
                    total++;
                }
            }
        }

        for (int i = 0; i < m; i++) {
            if (total < n && answer[i] < teamCnt[i]) {
                total++;
                answer[i]++;
            }
        }

        for (int cnt : answer) {
            System.out.println(cnt);
        }
    }
}

처음에는 한 번에 처리하려다가 문제가 생겨서 wave마다 계산하고 이번 wave를 더했을 때 보다 크면 하나씩 더해주는 것이 좋다는 힌트를 보고 해결했습니다