문제출저
https://www.acmicpc.net/problem/11286
11286번: 절댓값 힙
첫째 줄에 연산의 개수 N(1≤N≤100,000)이 주어진다. 다음 N개의 줄에는 연산에 대한 정보를 나타내는 정수 x가 주어진다. 만약 x가 0이 아니라면 배열에 x라는 값을 넣는(추가하는) 연산이고, x가 0
www.acmicpc.net
소스코드
package studyGroup.June.june15;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.PriorityQueue;
public class 백준11286절대값힙 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
// 우선순위큐 이중정렬 절대값 내림차순, 절대값이 같으면 일반값 내림차순
PriorityQueue<dot> pq = new PriorityQueue<>(new Comparator<dot>() {
@Override
public int compare(dot o1, dot o2) {
if(o1.abs == o2.abs)
{
return o1.val - o2.val;
}
return o1.abs - o2.abs;
}
});
ArrayList<Integer> answer = new ArrayList<>();
for(int i = 0; i < n; i++)
{
int input = Integer.parseInt(br.readLine());
if(input == 0)
{
if(pq.size() == 0)
{
answer.add(0);
}
else
{
int val = pq.poll().val;
answer.add(val);
}
}
else
{
pq.add(new dot(Math.abs(input), input));
}
}
for (Integer integer : answer) {
System.out.println(integer);
}
}
public static class dot
{
int abs;
int val;
dot(int abs, int val)
{
this.abs = abs;
this.val = val;
}
}
}