Skip to content

Commit 7386627

Browse files
feat : 백준 절댓값 힙
``` [절댓값 힙](https://www.acmicpc.net/problem/11286) ```
1 parent 76cfade commit 7386627

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package sgyj.backjun.seunggu;
2+
3+
import java.io.BufferedReader;
4+
import java.io.IOException;
5+
import java.io.InputStreamReader;
6+
import java.util.PriorityQueue;
7+
8+
public class Main11286 {
9+
10+
public static void main(String[] args) {
11+
try( BufferedReader reader = new BufferedReader(new InputStreamReader(System.in))) {
12+
PriorityQueue<AbsNumber> queue = new PriorityQueue<>();
13+
int count = Integer.parseInt(reader.readLine());
14+
StringBuilder stringBuilder =new StringBuilder();
15+
for(int i=0; i<count; i++) {
16+
int num = Integer.parseInt(reader.readLine());
17+
if(num == 0) {
18+
if(queue.isEmpty()) {
19+
stringBuilder.append(0);
20+
} else {
21+
stringBuilder.append(queue.poll().num);
22+
}
23+
stringBuilder.append("\n");
24+
} else {
25+
queue.offer(AbsNumber.of(num));
26+
}
27+
}
28+
System.out.println(stringBuilder);
29+
} catch (IOException e) {
30+
e.printStackTrace();
31+
}
32+
}
33+
34+
static class AbsNumber implements Comparable<AbsNumber> {
35+
private final int num;
36+
37+
private AbsNumber(int num) {
38+
this.num = num;
39+
}
40+
41+
public static AbsNumber of(int num) {
42+
return new AbsNumber(num);
43+
}
44+
45+
@Override
46+
public int compareTo(AbsNumber a) {
47+
if(Math.abs(this.num) == Math.abs(a.num)) return this.num - a.num;
48+
return Math.abs(this.num) - Math.abs(a.num);
49+
}
50+
}
51+
52+
}

0 commit comments

Comments
 (0)