Algorithm/백준
[백준] 1927 최소 힙 with C++
nowkoes
2023. 5. 22. 00:00
문제설명

입출력 예제

개념
지난 게시글인 최대 힙 문제처럼 우선순위 큐를 이용하여 풀면 쉽게 해결할 수 있다. 즉, 우선순위 큐를 최소 힙으로 정렬하면 된다.
풀이
#include <iostream>
#include <queue>
using namespace std;
int main()
{
ios::sync_with_stdio(false); cin.tie(nullptr);
int N, n;
priority_queue<int, vector<int>, greater<int>> pq;
cin >> N;
while (N--)
{
cin >> n;
if (n == 0)
{
if (pq.empty())
printf("0\n");
else
{
printf("%d\n", pq.top());
pq.pop();
}
}
else
pq.push(n);
}
}
반응형