알고리즘/백준
[백준] 11279번 - 최대 힙
땀두
2022. 3. 21. 09:06

힙을 사용하는 문제이다. 이 문제는 힙을 직접 구현해도 되지만 간단히 우선순위 큐를 이용하여서 해결하면 된다. 입력값이 0일 때 마다 출력을 해주는데 값이 없는 경우 0을 출력하고, 있으면 그 값들 중 최대값을 출력해주면 된다.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Collections;
import java.util.PriorityQueue;
public class p11279 {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
PriorityQueue<Integer> pq = new PriorityQueue<>(Collections.reverseOrder());
int a = Integer.parseInt(br.readLine());
for (int i = 0; i < a; i++) {
int b = Integer.parseInt(br.readLine());
if (b == 0) {
if (pq.isEmpty()) {
System.out.println(0);
} else {
System.out.println(pq.poll());
}
} else {
pq.add(b);
}
}
}
}