백준 11286번 절댓값 힙 JAVA

YB·2024년 9월 21일

링크텍스트

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));
    StringBuilder sb = new StringBuilder(); 

    PriorityQueue<Integer> pq = new PriorityQueue<>(new Comparator<Integer>() {
        @Override
            public int compare(Integer o1, Integer o2){
                if(Math.abs(o1)>Math.abs(o2)){
                    return Math.abs(o1)-Math.abs(o2);
                }else if(Math.abs(o1)==Math.abs(o2)){
                    return o1-o2;
                }else
                    return -1;
            }
        
    });

    int n = Integer.parseInt(br.readLine());

    for(int i=0;i<n;i++){
        int num = Integer.parseInt(br.readLine());

        if(num==0){
            if(pq.isEmpty()){
                sb.append(0+"\n");
            }else
                sb.append(pq.poll()+"\n");
        }else
            pq.add(num);
    }

        System.out.print(sb);

    }
}

profile
안녕하세요

0개의 댓글