本题是一个优先队列的水题,根据题目需要,我用到了pair(pair的用法可自行查找博客),pair在优先队列中按照先比较第一个元素,如果第一个相等在比较第二个(默认的是最大堆),但根据题目需要,要改成第一个元素由大到小,如果第一个元素相等,则第二个元素由小到大排序,因此要自定义pair的排序方式
AC代码
#include<bits/stdc++.h>
using namespace std;
#define MAXSIZE 1000000
//自定义排序方式
struct cmp {
bool operator()(pair<int,int>p1,pair<int,int>p2) {
if(p1.first == p2.first)
return p1.second>p2.second;
return p1.first<p2.first;
}
};
int main() {
int n;
while(scanf("%d",&n)!=EOF) {
//最大堆
priority_queue<pair<int,int>,vector<pair<int,int> >,cmp >q[4];
pair<int,int> p;
char word[5];
int x,y;
int ok = 1;
for(int i = 0; i < n; i++) {
cin>>word;
if(strcmp(word,"IN") == 0) {
cin>>x>>y;
p.first = y;
p.second = ok++;
q[x].push(p);
} else {
cin>>x;
if(!q[x].empty()) {
cout<<q[x].top().second<<endl;
q[x].pop();
} else
cout<<"EMPTY"<<endl;
}
}
}
return 0;
}