题目链接:https://www.jisuanke.com/course/615/28120
题目思路:STL之映射二叉堆
个人总结用法:让你查找堆中的某个元素的时候,让你同时取堆顶或者堆低的元素的时候,需要用到映射二叉堆。二叉堆中pair的第一个元素是决定优先级的!!!!!set中的end函数并不是最后一个元素,取最后的元素用的是rbegin。
ac代码:
#include <bits/stdc++.h>
using namespace std;
#define PII pair<int,int>
set<PII,greater<PII> >gheap;//声明,建堆,大根堆 less小根堆
int main()
{
int x;
while(cin>>x){
if(x==0) break;
if(x==1){
int a,b;cin>>a>>b;//B是决定数据优先级的
gheap.insert(make_pair(b,a));//插入
}
if(x==2){
if(gheap.size()==0) cout<<"0"<<endl;
else{
cout<<gheap.begin()->second<<endl;
gheap.erase(*(gheap.begin()));//删除
}
}
if(x==3){
if(gheap.size()==0) cout<<"0"<<endl;
else{
cout<<gheap.rbegin()->second<<endl;
gheap.erase(*(gheap.rbegin()));
}
}
}
}