우보천리 개발

[백준11286] 절댓값 힙 Java 본문

카테고리 없음

[백준11286] 절댓값 힙 Java

밥은답 2023. 8. 17. 21:36
반응형

https://www.acmicpc.net/problem/11286

 

11286번: 절댓값 힙

첫째 줄에 연산의 개수 N(1≤N≤100,000)이 주어진다. 다음 N개의 줄에는 연산에 대한 정보를 나타내는 정수 x가 주어진다. 만약 x가 0이 아니라면 배열에 x라는 값을 넣는(추가하는) 연산이고, x가 0

www.acmicpc.net

 

package Chapter1;

import java.util.*;

public class BJ11286 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        PriorityQueue<Integer> q = new PriorityQueue<>(new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                if (Math.abs(o1) == Math.abs(o2)) {
                    return o1-o2;
                }
                else {
                    return Math.abs(o1) - Math.abs(o2);
                }
            }
        });

        for (int i=0; i<n; i++) {
            int query = sc.nextInt();
            if (query != 0) {
                q.offer(query);
            }
            if (query == 0) {
                if (q.isEmpty()) {
                    System.out.println(0);
                }
                else {
                    System.out.println(q.poll());
                }
            }
        }
    }
}
반응형
Comments