- toc {:toc}
π¦7662: PriorityQueue
[μκ³ λ¦¬μ¦ νμ΄] κ°λκ·
Reviewed by Kade Kang (devkade12@gmail.com)
Reviewed:: 2024-02-05
λ¬Έμ νμΈνκΈ°
νμ΄
- priority_queue λ₯Ό μ¬μ©ν΄μ νμ΄νλ€.
- max, min μ°μ μμ νλ₯Ό λ§λ λ€.
- push ν λλ λͺ¨λ min, max λͺ¨λμ push νλ€.
- pop ν λ -1 μΌ λλ min, 1 μΌ λλ max μμ pop νλ€. μ΄λ κ°κ° pop νμ§ μμ ν (1 μΌ λλ min, -1 μΌ λλ max) μμλ μ¬μ ν κ°μ΄ λ¨μ μμ μ μλ€. μ΄λ₯Ό 체ν¬νκ³ μ κ±°ν΄μ€λ€.
#include <iostream>
#include <queue>
#include <map>
#define endl '\n'
using namespace std;
priority_queue<int> max_q;
priority_queue<int, vector<int>, greater<int>> min_q;
map<int, int> ckpt;
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
int T, k, val;
char ch;
cin >> T;
while(T--){
while(!max_q.empty()) max_q.pop();
while(!min_q.empty()) min_q.pop();
ckpt.clear();
cin >> k;
while(k--){
cin >> ch >> val;
if (ch=='I'){
max_q.push(val);
min_q.push(val);
ckpt[val]++;
}
else{
if (val==1){
if (!max_q.empty()){
ckpt[max_q.top()]--;
max_q.pop();
}
}
else{
if (!min_q.empty()){
ckpt[min_q.top()]--;
min_q.pop();
}
}
while(!min_q.empty() && ckpt[min_q.top()]==0)
min_q.pop();
while(!max_q.empty() && ckpt[max_q.top()]==0)
max_q.pop();
}
}
if (max_q.empty() || min_q.empty()) {
cout << "EMPTY" << endl;
}
else{
cout << max_q.top() << ' ' << min_q.top() << endl;
}
}
return 0;
}