1. 문제

2. 아이디어
3. 코드
import java.util.*;
import java.io.*;
class Main {
public static void main(String args[]) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuffer sb = new StringBuffer();
int N = Integer.parseInt(br.readLine());
PriorityQueue<Integer> pq = new PriorityQueue<>(new Comparator<Integer>(){
@Override
public int compare(Integer a, Integer b){
if (Math.abs(a) != Math.abs(b)){
return Math.abs(a) - Math.abs(b);
}else{
return a - b;
}
}
});
int tmp_num;
for(int i = 0 ; i < N;i++){
tmp_num = Integer.parseInt(br.readLine());
if (tmp_num != 0){
pq.offer(tmp_num);
}else{
if(pq.size() == 0){
sb.append("0");
}
else{
sb.append(pq.poll());
}
sb.append("\n");
}
}
System.out.println(sb);
}
}
4. 느낀점 및 배운점
PriorityQueue<타입명> pq = new PriorityQueue<>(new Comparator<Integer>(){
@Override
public int compare(타입명 a, 타입명 b){
return 조건;
}
});
pq.offer(값);
pq.poll();
- StringBuffer 로 쉽게 문자열의 처리가 가능하다.