반응형
<코드>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
int N, x;
priority_queue<int,vector<int>,greater<int>> pq;
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> N;
for (int i = 0; i < N; i++)
{
cin >> x;
if (x == 0)
{
if (pq.empty()) printf("0\n");
else
{
printf("%d\n", pq.top());
pq.pop();
}
}
else pq.push(x);
}
}
풀이 방법
[C/C++] 백준 11279번 - 최대 힙 (우선순위 큐)
<코드> #include #include #include using namespace std; int N, x; priority_queue pq; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> N; for (int i = 0; i < N; i++) { cin >> x; if (x =..
cocoon1787.tistory.com
이 문제를 풀었던 코드와 동일하지만 우선순위 큐의 default값이 내림차순이기 때문에 아래와같이 오름차순으로 정의해줍니다.
priority_queue<int,vector<int>,greater<int>> pq;
1927번: 최소 힙
첫째 줄에 연산의 개수 N(1 ≤ N ≤ 100,000)이 주어진다. 다음 N개의 줄에는 연산에 대한 정보를 나타내는 정수 x가 주어진다. 만약 x가 자연수라면 배열에 x라는 값을 넣는(추가하는) 연산이고, x가 0
www.acmicpc.net
반응형
'🧩PS > 🥈Nomal' 카테고리의 다른 글
[C/C++] 백준 1655번 - 가운데를 말해요 (최대 힙, 최소 힙) (0) | 2020.12.28 |
---|---|
[C/C++] 백준 11286번 - 절댓값 힙 (우선순위 큐) (0) | 2020.12.27 |
[C/C++] 백준 11279번 - 최대 힙 (우선순위 큐) (0) | 2020.12.27 |
[C/C++] 백준 2293번 - 동전 1 (DP) (0) | 2020.12.25 |
[C/C++] 백준 10826번 - 피보나치 수 4 (0) | 2020.12.23 |