땀두 블로그

[백준] 1927번 - 최소 힙 본문

알고리즘/백준

[백준] 1927번 - 최소 힙

땀두 2022. 3. 21. 09:09

 

위 주소의 문제와 같은 문제로, 위 문제에서는 최대값부터 출력하기 위해 Collections.reverseOrder를 사용하였다면 그 부분만 제거하면 쉽게 해결할 수 있다.

 

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

public class p1927 {

	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<>();
		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);
			}
		}
	}
}
 

 

 

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

[백준] 18870번 - 좌표 줄이기  (0) 2022.03.21
[백준] 11286번 - 절댓값 힙  (0) 2022.03.21
[백준] 11279번 - 최대 힙  (0) 2022.03.21
[백준] 1931번 - 회의실 배정  (0) 2022.03.21
[백준] 11726번 - 2*n 타일링  (0) 2022.03.21
Comments