- 최대힙
https://www.acmicpc.net/problem/11279
#include <iostream>
#include <queue>
using namespace std;
int main(){
cin.tie(0);
cin.sync_with_stdio(0);
int n, inp;
cin >> n;
priority_queue<int> h;
while(n--){
cin >> inp;
if (inp == 0){
if (h.empty()){
cout<<"0\n";
}
else{
cout<<h.top()<<"\n";
h.pop();
}
}
else h.push(inp);
}
}
- 최소힙
https://www.acmicpc.net/problem/1927
#include <iostream>
#include <queue>
using namespace std;
int main(){
cin.tie(0);
cin.sync_with_stdio(0);
int n, inp;
cin >> n;
// priority_queue<int> h;
priority_queue<int, vector<int>, greater<int> >h;
while(n--){
cin >> inp;
if (inp == 0){
if (h.empty()){
cout<<"0\n";
}
else{
cout<<h.top()<<"\n";
h.pop();
}
}
else h.push(inp);
}
}
- 처음 두문장은 입출력 및 실행시간을 최소화 시켜준다
- 이 문제는 이런 간략화 작업이 없으면 시간초과가 발생했다
- 두 문제 모두 우선순위 큐로 풀어주었다
- 두 문제는 한 줄을 제외하고는 모두 같다
- 최소힙은 최대힙과는 다르게 오름차순이기 때문에 큐를 선언할 때 greater 옵션을 줘야 한다
'자기개발👨💻 > 코딩 알고리즘' 카테고리의 다른 글
[C++] 백준 1182 부분수열의 합 (0) | 2022.03.30 |
---|---|
[C++] 백준 1436 영화감독 숌 (0) | 2022.03.23 |
[C++] 백준 1018 체스판 다시 칠하기 (0) | 2022.03.23 |
[C++] 백준 1158 요세푸스 문제 //<queue> (0) | 2022.03.18 |
[C++] 백준 11651 // <vector><map> (0) | 2022.03.16 |
[C++] 백준 1302 베스트셀러 // <map> (0) | 2022.03.16 |