Skip to content

Commit 99eb43d

Browse files
committed
[Silver II] Title: 최대 힙, Time: 804 ms, Memory: 33200 KB -BaekjoonHub
1 parent 55365e0 commit 99eb43d

File tree

2 files changed

+74
-0
lines changed

2 files changed

+74
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# [Silver II] 최대 힙 - 11279
2+
3+
[문제 링크](https://www.acmicpc.net/problem/11279)
4+
5+
### 성능 요약
6+
7+
메모리: 33200 KB, 시간: 804 ms
8+
9+
### 분류
10+
11+
자료 구조, 우선순위 큐
12+
13+
### 제출 일자
14+
15+
2024년 11월 13일 14:02:05
16+
17+
### 문제 설명
18+
19+
<p>널리 잘 알려진 자료구조 중 최대 힙이 있다. 최대 힙을 이용하여 다음과 같은 연산을 지원하는 프로그램을 작성하시오.</p>
20+
21+
<ol>
22+
<li>배열에 자연수 x를 넣는다.</li>
23+
<li>배열에서 가장 큰 값을 출력하고, <span style="line-height:1.6em">그 값을 배열에서 제거한다. </span></li>
24+
</ol>
25+
26+
<p><span style="line-height:1.6em">프로그램은 처음에 비어있는 배열에서 시작하게 된다.</span></p>
27+
28+
### 입력
29+
30+
<p>첫째 줄에 연산의 개수 N(1 ≤ N ≤ 100,000)이 주어진다. 다음 N개의 줄에는 연산에 대한 정보를 나타내는 정수 x가 주어진다. 만약 x가 자연수라면 배열에 x라는 값을 넣는(추가하는) 연산이고, x가 0이라면 배열에서 가장 큰 값을 출력하고 그 값을 배열에서 제거하는 경우이다. 입력되는 자연수는 2<sup>31</sup>보다 작다.</p>
31+
32+
### 출력
33+
34+
<p>입력에서 0이 주어진 횟수만큼 답을 출력한다. 만약 배열이 비어 있는 경우인데 가장 큰 값을 출력하라고 한 경우에는 0을 출력하면 된다.</p>
35+
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import java.io.BufferedReader;
2+
import java.io.InputStreamReader;
3+
import java.io.IOException;
4+
import java.util.Collection;
5+
import java.util.Collections;
6+
import java.util.PriorityQueue;
7+
import java.util.StringTokenizer;
8+
9+
public class Main {
10+
11+
public static long C;
12+
13+
public static void main(String[] args) throws IOException {
14+
15+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
16+
17+
int N = Integer.parseInt(br.readLine());
18+
19+
PriorityQueue<Integer> pq = new PriorityQueue<Integer>(Collections.reverseOrder());
20+
21+
for(int i =0; i<N; i++) {
22+
23+
int cur = Integer.parseInt(br.readLine());
24+
25+
if(cur ==0 ) {
26+
if(!pq.isEmpty()) {
27+
System.out.println(pq.poll());
28+
}
29+
else
30+
System.out.println(0);
31+
}
32+
else {
33+
pq.add(cur);
34+
}
35+
}
36+
}
37+
38+
39+
}

0 commit comments

Comments
 (0)