Skip to content

Commit 1de57d3

Browse files
committed
[Silver II] Title: 최대 힙, Time: 64 ms, Memory: 2800 KB -BaekjoonHub
1 parent 237da8d commit 1de57d3

2 files changed

Lines changed: 68 additions & 0 deletions

File tree

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+
메모리: 2800 KB, 시간: 64 ms
8+
9+
### 분류
10+
11+
자료 구조, 우선순위 큐
12+
13+
### 제출 일자
14+
15+
2025년 12월 2일 19:47:00
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: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#include <iostream>
2+
#include <queue>
3+
#include <vector>
4+
using namespace std;
5+
6+
int N, X;
7+
8+
int main() {
9+
vector<int> result;
10+
11+
cin >> N;
12+
13+
priority_queue<int> q;
14+
for (int i = 0; i < N; i++) {
15+
cin >> X;
16+
if (X != 0) {
17+
q.push(X);
18+
} else {
19+
if (q.empty()) {
20+
result.push_back(0);
21+
} else {
22+
result.push_back(q.top());
23+
q.pop();
24+
}
25+
}
26+
}
27+
28+
for (int i = 0; i < result.size(); i++) {
29+
cout << result[i] << "\n";
30+
}
31+
32+
return 0;
33+
}

0 commit comments

Comments
 (0)