문제
https://www.acmicpc.net/problem/1927
1927번: 최소 힙
첫째 줄에 연산의 개수 N(1 ≤ N ≤ 100,000)이 주어진다. 다음 N개의 줄에는 연산에 대한 정보를 나타내는 정수 x가 주어진다. 만약 x가 자연수라면 배열에 x라는 값을 넣는(추가하는) 연산이고, x가 0
www.acmicpc.net
소스코드
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <queue>
using namespace std;
priority_queue<int, vector<int>,greater<int>> pq;
int main() {
ios_base::sync_with_stdio(false), cin.tie(NULL);
int n;
cin >> n;
for (int i = 0; i < n; i++) {
int x;
cin >> x;
if (x == 0) {
if (pq.empty()) {
cout << "0" << "\n";
}
else {
int ans = pq.top();
cout << ans << "\n";
pq.pop();
}
}
else {
pq.push(x);
}
}
}
해설
우선순위 큐를 이용해서 문제를 해결할 수 있다.
'Algorithm > 자료구조' 카테고리의 다른 글
[백준] 알고리즘 C++ 5430번 - AC문제 (0) | 2022.01.08 |
---|---|
[백준] 알고리즘 C++ 17298번 - 오큰수 문제 (0) | 2022.01.07 |
[백준] 알고리즘 C++ 1764번 - 듣보잡문제 (0) | 2021.11.19 |
[백준] 알고리즘 C++ 10989번 - 수 정렬하기3문제 (0) | 2021.11.10 |
[백준] 알고리즘 C++ 11866번 - 요세푸스 문제 0문제 (0) | 2021.11.07 |