알고리즘

C++ 알고리즘 - 백준 1927 최소 힙 (우선순위 큐 오름/내림차순)

마루설아 2025. 1. 9. 19:56

https://www.acmicpc.net/problem/1927

 

#include <bits/stdc++.h>
#define endl "\n"

using namespace std;

int main(void) {
	/************** C++ Init **************/
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	/************** C++ Init **************/


	int input1, input2;
	priority_queue<int, vector<int>, greater<int>> pq;

	cin >> input1;

	for (int i = 0; i < input1; i++) {
		cin >> input2;
		if (input2 == 0) {
			if (pq.empty()) cout << 0 << endl;

			else {
				int a = pq.top();
				pq.pop();
				cout << a << endl;
			}
		}

		else pq.push(input2);
	}
}

 

우선순위 큐 내림차순 (기본값)

priority_queue<int, vector<int>, less<int>>

 => priority_queue<int>

 

우선순위 큐 오름차순

priority_queue<int, vector<int>, greater<int>>