📍 문제
📍 우선순위 큐 (priority_queue)
- 오름차순과 내림차순과 같은 정렬 기능이 들어간 queue
- 우선순위 큐는 각 원소들이 우선순위를 갖는다.
- priority_queue는 기본적으로 내림차순으로 정렬
- priority_queue<자료형, 구현체, 비교연산자>
- 비교 연산자에는 less<자료형>, greater<자료형>이 있음
- less : 내림차순 ( 큰 -> 작은)
- greater : 오름차순 ( 작은 -> 큰)
오름차순 정렬을 위해서는 다음과 같이 정렬해야 한다.
priority_queue<int, vector<int>, greater<int>> q; // 오름차순
📍 코드
#include<iostream>
#include<queue>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int N = 0;
int x = 0;
priority_queue<int, vector<int>, greater<int>> q;
cin >> N;
for (int i{ 0 }; i < N; i++) {
cin >> x;
if (x == 0) {
if (q.size() == 0) {
cout << 0 << "\n";
}
else {
cout << q.top() << "\n";
q.pop();
}
}
else {
q.push(x);
}
}
}
'백준 & 프로그래머스' 카테고리의 다른 글
[BOJ/백준] C++ 2512번 예산 (0) | 2023.09.02 |
---|---|
[C++ STL] 우선순위 큐 (priority queue) (0) | 2023.07.13 |
[프로그래머스] Lv.2 가장 큰 수 C++ (0) | 2023.07.10 |
[프로그래머스] Lv.2 주식 가격 C++ (0) | 2023.07.07 |
[프로그래머스] Lv.2 올바른 괄호 C++ _ <더 간단한 풀이> (0) | 2023.07.07 |