땀두 블로그

[백준] 11286번 - 절댓값 힙 본문

알고리즘/백준

[백준] 11286번 - 절댓값 힙

땀두 2022. 3. 21. 09:12

 

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

public class p11286 {

	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		PriorityQueue<Integer> pq = new PriorityQueue<>(new Comparator<Integer>() {
			@Override
			public int compare(Integer a, Integer b) {
				if (Math.abs(a) > Math.abs(b) || Math.abs(a) == Math.abs(b) && a > b) {
					return 1;
				} else {
					return -1;
				}
			}
		});
		int a = Integer.parseInt(br.readLine());

		for (int i = 0; i < a; i++) {
			int b = Integer.parseInt(br.readLine());
			if (b == 0) {
				if (pq.isEmpty()) {
					System.out.println(0);
				} else {
					System.out.println(pq.poll());
				}
			} else {
				pq.add(b);
			}
		}
	}
}
 

 

 

'알고리즘 > 백준' 카테고리의 다른 글

[백준] 11724번 - 연결 요소의 개수  (0) 2022.03.21
[백준] 18870번 - 좌표 줄이기  (0) 2022.03.21
[백준] 1927번 - 최소 힙  (0) 2022.03.21
[백준] 11279번 - 최대 힙  (0) 2022.03.21
[백준] 1931번 - 회의실 배정  (0) 2022.03.21
Comments