라이브러리에 있는 우선순위 큐를 활용한다. 단 라이브러리 우선순위 큐는 최대 힙이 기본이므로 최소 힙으로 바꾸기 위해서는 선언을 따로 해주어야한다.
std::priority_queue<int, std::vector<int>, std::greater<int>> pq;
이런 식으로 해주면 된다.
//백준 1927, 최소 힙
#include <iostream>
#include <queue>
#include <vector>
#include <functional>
std::priority_queue<int, std::vector<int>, std::greater<int>> pq;
int main (){
std::ios_base::sync_with_stdio(false);
std::cin.tie(NULL);
std::cout.tie(NULL);
int N, x;
std::cin >> N;
while(N--){
std::cin >> x;
if(x == 0){
if(pq.empty()) std::cout << 0 << '\n';
else{
std::cout << pq.top() << '\n';
pq.pop();
}
}
else{
pq.push(x);
}
}
return 0;
}
2025-02-09T01:38:25.098Z