https://www.acmicpc.net/problem/11286
문제 하단에 최소힙, 최대힙 을 보고 아이디어를 얻어서 풀었다
음수는 최대힙, 양수는 최소힙에 저장하고
꺼낼때는 절대값이 더 작은 쪽을 먼저 찾고 동일한 값이면 최대힙에서 제거하는 방식으로 해결하였다
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int inputCnt = Integer.parseInt(br.readLine());
PriorityQueue<Integer> max = new PriorityQueue<>((a,b)-> (b-a > 0) ? 1 : -1);//음수 처리
PriorityQueue<Integer> min = new PriorityQueue<>();//양수 처리
StringBuilder sb = new StringBuilder();
int tmp = 0;
for(int i = 0; i < inputCnt; ++i)
{
tmp = Integer.parseInt(br.readLine());
if( 0 == tmp )
{
if((max.isEmpty() == false) && (min.isEmpty() == false))
{
if( Math.abs(max.peek()) <= min.peek())
{
sb.append(max.poll()+"\n");
}
else if(Math.abs(max.peek()) > min.peek())
{
sb.append(min.poll()+"\n");
}
}
else if(max.isEmpty() == false)
{
sb.append(max.poll()+"\n");
}
else if(min.isEmpty() == false)
{
sb.append(min.poll()+"\n");
}
else
{
sb.append("0"+"\n");
}
}
else
{
if(tmp < 0)
{
max.add(tmp);
}
else if(tmp > 0)
{
min.add(tmp);
}
}
}
System.out.println(sb.toString());
}
}