https://www.acmicpc.net/problem/1927
1927번: 최소 힙
첫째 줄에 연산의 개수 N(1 ≤ N ≤ 100,000)이 주어진다. 다음 N개의 줄에는 연산에 대한 정보를 나타내는 정수 x가 주어진다. 만약 x가 자연수라면 배열에 x라는 값을 넣는(추가하는) 연산이고, x가 0
www.acmicpc.net
걸린 시간 : 5분 18초
정답 코드
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.PriorityQueue;
public class BOJ1927 {
static PriorityQueue<Integer> pq = new PriorityQueue<>();
static int N;
public static void main(String[] args) throws IOException {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
N = Integer.parseInt(bf.readLine());
for (int i = 0; i < N; i++) {
int input = Integer.parseInt(bf.readLine());
if (input == 0) {
if (pq.isEmpty()) {
System.out.println(0);
} else {
System.out.println(pq.poll());
}
} else {
pq.add(input);
}
}
}
}
'Baekjoon Online Judge' 카테고리의 다른 글
[BOJ] 14940 : 쉬운 최단거리 (Java) (0) | 2023.11.09 |
---|---|
[BOJ] 2630 : 색종이 만들기 (Java) with 분할 정복 (1) | 2023.11.09 |
[BOJ] 11444 : 피보나치 수 6 (Java) (0) | 2023.11.02 |
[BOJ] 9251 : LCS (Java) (0) | 2023.11.02 |
[BOJ] 1987 : 알파벳 (Java) (1) | 2023.10.26 |