用 priority_queue 與 unordered_map 。
#include<bits/stdc++.h>
using namespace std;
int main(){
ios::sync_with_stdio(false);cin.tie(0);
priority_queue<int> mx;
priority_queue<int,vector<int>,greater<int>> mn;
unordered_map<int,int> s;
int t;
while(cin>>t){
if(t==0) break;
if(t==-1){
while(!mn.empty() && !s[mn.top()]){
mn.pop();
}
if(!mn.empty()){
cout<<mn.top()<<' ';
s[mn.top()]--;
mn.pop();
}
}else if(t==-2){
while(!mx.empty() && !s[mx.top()]){
mx.pop();
}
if(!mx.empty()){
cout<<mx.top()<<' ';
s[mx.top()]--;
mx.pop();
}
}else{
s[t]++;
mx.push(t);
mn.push(t);
}
}
return 0;
}