https://www.acmicpc.net/problem/11286
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Comparator;
import java.util.PriorityQueue;
public class Main{
public static class Element{
int original;
int ab;
public Element(int original){
this.original=original;
this.ab=Math.abs(original);
}
public int getAbsValue(){
return ab;
}
public int getOriginal(){
return original;
}
}
public static void solution(int[]arr, int n){
PriorityQueue<Element>pq=new PriorityQueue<>(Comparator.comparing(Element::getAbsValue).thenComparing(Element::getOriginal));
for(int i=0;i<n;++i){
if(arr[i]!=0){
pq.add(new Element(arr[i]));
}
else{
if(pq.isEmpty())
System.out.println(0);
else {
int output = pq.poll().original;
System.out.println(output);
}
}
}
}
public static void main(String[] args)throws Exception{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int n=Integer.parseInt(br.readLine());
int[]arr=new int[n];
for(int i=0;i<n;++i){
arr[i]=Integer.parseInt(br.readLine());
}
solution(arr,n);
}
}
'알고리즘 > 자료구조유형(알고리즘 문제)' 카테고리의 다른 글
백준(BOJ) 20040: 사이클 게임 (0) | 2024.02.05 |
---|---|
leetcode[735] Asteroid Collision (1) | 2023.12.23 |