[ 문제 ]
절댓값 힙은 다음과 같은 연산을 지원하는 자료구조이다.
1. 배열에 정수 x (x ≠ 0)를 넣는다.
2. 배열에서 절댓값이 가장 작은 값을 출력하고, 그 값을 배열에서 제거한다. 절댓값이 가장 작은 값이 여러개일 때는, 가장 작은 수를 출력하고, 그 값을 배열에서 제거한다.
package heap;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.Queue;
public class bj11286 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(br.readLine());
//우선순위 큐의 커스터마이징 방법 -> Comparator 사용하기
Queue<Integer> ans = new PriorityQueue<>(new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
int A = Math.abs(o1);
int B = Math.abs(o2);
if(A > B){
return A - B;
} else if(A == B){
if(o1 > o2){
return 1;
} else {
return -1;
}
} else{
return -1;
}
}
});
for (int i = 0; i < N; i++) {
int x = Integer.parseInt(br.readLine());
if( x == 0){
if(ans.isEmpty()){
System.out.println(0);
} else{
System.out.println(ans.remove());
}
} else {
ans.add(x);
}
}
}
}