-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBoj_1927.java
More file actions
51 lines (41 loc) · 1.31 KB
/
Boj_1927.java
File metadata and controls
51 lines (41 loc) · 1.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Comparator;
import java.util.PriorityQueue;
public class Boj_1927 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
PriorityQueue<Integer> pq = new PriorityQueue<>(comp);
/* 클래스 따로 안 하고 한 번에 해도 됨
* PriorityQueue<Integer> pq = new PriorityQueue<>(new Comparator<Integer>() {
* @Override
* public int compare(Integer o1, Integer o2) {
* return o1.compareTo(o2);
* }
* } );
*/
int N = Integer.parseInt(br.readLine());
int[] arr = new int[N];
StringBuilder sb = new StringBuilder();
for(int i=0;i<N;i++) {
arr[i] = Integer.parseInt(br.readLine());
if(arr[i] == 0) {
if(pq.isEmpty())
sb.append(0 + "\n");
else
sb.append(pq.poll() + "\n"); // 가장 큰 값 출력
} else {
pq.add(arr[i]);
}
}
System.out.println(sb);
}
// 비교 배열 (최소힙이므로 오름차순으로) (o1 이 o2 보다 크면 양수 / 같으면 0 / 작으면 음수 반환)
public static Comparator<Integer> comp = new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
return o1-o2;
}
};
}