728x90
https://www.acmicpc.net/problem/11279
11279번: 최대 힙
첫째 줄에 연산의 개수 N(1 ≤ N ≤ 100,000)이 주어진다. 다음 N개의 줄에는 연산에 대한 정보를 나타내는 정수 x가 주어진다. 만약 x가 자연수라면 배열에 x라는 값을 넣는(추가하는) 연산이고, x가
www.acmicpc.net
자바에는 PriorityQueue 가 있으므로 이를 이용하자.
직접 구현하려다 너무 복잡해서 찾아보니 자바 내에 이미 존재했다..
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Collections;
import java.util.PriorityQueue;
public class bj11279 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(br.readLine());
int x;
PriorityQueue<Integer> q = new PriorityQueue<>(Collections.reverseOrder());
for(int i = 0; i < N; i ++) {
x = Integer.parseInt(br.readLine());
if(x == 0) {
if (q.isEmpty()) System.out.println(0);
else System.out.println(q.poll());
}
else q.add(x);
}
}
}728x90
'알고리즘 > 백준' 카테고리의 다른 글
| 백준 1303 자바 풀이 (0) | 2022.03.10 |
|---|---|
| 백준 2501 자바 풀이 (0) | 2022.03.08 |
| 백준 1697 자바 풀이 (0) | 2022.03.08 |
| 백준 2620 자바 풀이 (0) | 2022.03.06 |
| 백준 1260 자바 풀이 (0) | 2022.03.06 |