알고리즘/힙(우선순위큐)

[백준] 11279번 최대 힙

VIPeveloper 2022. 5. 17. 01:06
728x90
반응형
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Collections;
import java.util.PriorityQueue;

public class Main {

    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        // 가장 큰 수일수록 앞에 나온다.
        PriorityQueue<Integer> pq = new PriorityQueue<>(Collections.reverseOrder());
        int n = Integer.parseInt(br.readLine());
        for (int i = 0; i < n; i++) {
            int num = Integer.parseInt(br.readLine());
            if(num == 0){
                if(!pq.isEmpty()) {
                    System.out.println(pq.poll());
                } else System.out.println(0);
            }else{
                pq.offer(num);
            }
        }
    }
}
728x90
반응형