dev.Log

11286 우선순위큐에 우선순위 적용하기 본문

알고리즘

11286 우선순위큐에 우선순위 적용하기

초코푸딩 2022. 9. 13. 14:20
package acmicpc;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.PriorityQueue;

public class P11286_절대값힙 {
    public static void main(String[] args) throws IOException {
        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
        int N = Integer.parseInt(bf.readLine());
        PriorityQueue<Integer> que = new PriorityQueue<>((o1,o2) -> {
            int first_abs = Math.abs(o1);
            int second_abs = Math.abs(o2);
            if(first_abs == second_abs) return o1 > o2 ? 1 : -1; //절대값이 같으면 음수 우선 정렬하기
            else return first_abs - second_abs; //절대값을 기준으로 정렬하기
        });
        for(int i=0; i<N ; i++) {
            int request = Integer.parseInt(bf.readLine());
            if(request == 0) {
                if(que.isEmpty()) {
                    System.out.println("0");
                } else {
                    System.out.println(que.poll());
                }
            } else {
                que.add(request);
            }
        }
    }
}

'알고리즘' 카테고리의 다른 글

2750 버블정렬  (0) 2022.09.13
2164 큐  (0) 2022.09.13
17298 스택  (0) 2022.09.13
스택 P1874  (0) 2022.09.12
Deque<Node> deque = new LinkedList<>();  (0) 2022.09.12
Comments