\https://www.acmicpc.net/problem/11286
#11286: 절대값 힙
첫 번째 줄은 작업 수 N(1≤N≤100,000)을 제공합니다.
다음 N 줄은 작업에 대한 정보를 나타내는 정수 x를 제공합니다.
x가 0이 아니면 x 값을 배열에 삽입(더하기)하는 연산이고 x는 0
www.acmicpc.net
자바 코드
import java.util.Scanner;
import java.util.PriorityQueue;
import java.util.Comparator;
public class Main {
public static void main(String() args) {
Scanner sc = new Scanner(System.in);
StringBuilder sb = new StringBuilder();
PriorityQueue<Integer> q = new PriorityQueue<>(new Comparator<Integer>() {
public int compare(Integer o1, Integer o2) {
int a = Math.abs(o1);
int b = Math.abs(o2);
if(a == b) return o1 - o2;
else return a - b;
}
});
int n = sc.nextInt();
for(int i = 0; i < n; i++) {
int num = sc.nextInt();
if(num == 0) {
if(q.isEmpty()) sb.append(0+"\n");
else sb.append(q.poll()+"\n");
}
else q.add(num);
}
System.out.println(sb);
}
}