Playground/자바문제집

[백준] 11286번

미숫가루설탕많이 2023. 4. 7. 11:02

 

 
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
import java.util.*;

public class Main {
    public static void main(String[] args) throws IOException {

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int N = Integer.parseInt(br.readLine());    // 연산의 개수

        PriorityQueue<Integer> que = new PriorityQueue<>((o1, o2) -> {
            int firstAbs = Math.abs(o1);
            int secondAbs = Math.abs(o2);

            if (firstAbs == secondAbs) {    // 절대값이 같으면 음수 우선
                return o1 > o2 ? 1 : -1;
            }
            return firstAbs - secondAbs;    // 절대값이 다르면 작은 데이터 우선
        });

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

            if (request == 0) {
                if (que.isEmpty()) {
                    System.out.println("0");
                } else {
                    System.out.println(que.poll());
                }
            } else {
                que.add(request);
            }
        }
    }
}

'Playground > 자바문제집' 카테고리의 다른 글

[프로그래머스] 문자열 바꿔서 찾기  (0) 2023.05.09
[백준] 1427번  (0) 2023.04.10
[백준] 2164번  (0) 2023.04.07
[프로그래머스] 피보나치 수  (0) 2023.04.07
[백준] 1874번  (0) 2023.04.06