delpho

[실버 2, 우선순위 큐] 백준 11279 - 최대 힙 (자바) 본문

알고리즘/우선순위 큐

[실버 2, 우선순위 큐] 백준 11279 - 최대 힙 (자바)

delpho 2024. 2. 19. 15:51

Think

1. 컬렉션에 있는 우선순위 큐를 사용할 수 있으면 풀 수 있는 문제다.

 

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;

public class test {
    static int N;
    static PriorityQueue<Integer> pq = new PriorityQueue<>(Collections.reverseOrder());
    static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    static StringBuilder sb = new StringBuilder();

    public static void main(String[] args) throws IOException {
        init();

    }

    public static void init() throws IOException {
        N = Integer.parseInt(br.readLine());

        for (int i = 0; i < N; i++) {
            int x = Integer.parseInt(br.readLine());

            if(x == 0){
                int n = pq.isEmpty() ? 0 : pq.poll();
                sb.append(n).append('\n');
            }else{
                pq.add(x);
            }
        }
        sb.delete(sb.length()-1, sb.length());
        System.out.print(sb);
    }
}