백준1927 최소힙
힙
Last updated
힙
Last updated
package practice.algorithm.stackqueue;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.PriorityQueue;
/**
* https://www.acmicpc.net/problem/1927
* 26264 kb / 320 ms
* 최소힙
* - 미리 구현된 priority queue 자료형 사용
* */
public class Bj1927 {
private static final PriorityQueue<Integer> nums = new PriorityQueue<>();
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int round = Integer.parseInt(br.readLine());
StringBuilder sb = new StringBuilder();
for (int i = 0; i < round; i++) {
int result = minHeap(Integer.parseInt(br.readLine()));
if (result >= 0) {
sb.append(result).append("\n");
}
}
System.out.println(sb);
}
private static int minHeap(int num) {
if (num == 0) {
if (nums.isEmpty()) {
return 0;
}
return nums.poll();
}
nums.add(num);
return -1;
}
}