728x90
우선순위 큐 문제입니다.
1. 수가 0일때
- 비어있으면 0출력
- 비어있지 않으면 pq.top 출력, pq.pop
2. 수가 0이 아니면 pq.push
오름차순으로 출력하기 때문에 다음과 같이 선언해줍니다.
오름차순 형태
priority_priority_queue<int, vector<int>, greater<int> > pq;
기본 형태
priority_priority_queue<int> pq;
#include <iostream>
#include <string>
#include <algorithm>
#include <cstring>
#include <vector>
#include <queue>
using namespace std;
int main(int argc, char *argv[])
{
int n;
int num;
priority_queue<int, vector<int>, greater<int> > pq;
scanf("%d", &n);
while (n--) {
scanf("%d", &num);
if (num == 0) {
if (pq.empty()) {
printf("0\n");
}
else {
printf("%d\n", pq.top());
pq.pop();
}
}
else {
pq.push(num);
}
}
return 0;
}
'알고리즘' 카테고리의 다른 글
[C++] 백준 1655번 가운데를 말해요 (0) | 2019.09.10 |
---|---|
[C++] 백준 11286번 절댓값 힙 (0) | 2019.09.10 |
[C++] 백준 11279번 최대 힙 (0) | 2019.09.09 |
[C++] 백준 10816번 숫자 카드 2 (0) | 2019.09.09 |
[C++] 백준 1920번 수 찾기 (0) | 2019.09.09 |