這是一個優先隊列的簡單應用!
[cpp]
#include<iostream>
#include<string>
#include<cstring>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<cctype>
#include<iomanip>
#include<queue>
using namespace std;
const int inf=10000000;
struct node {
int num;
int k;
bool operator < (const node& x) const {
if(k!=x.k)return k<x.k;
else return num>x.num;
}
}rt,R;
int main() {
int n;
while(cin>>n) {
priority_queue<node>M[4];
for(int i=1,t=1; i<=n; ++i) {
string cmd; cin>>cmd;
if(cmd=="IN") {
int a,b; cin>>a>>b;
rt.num=t++, rt.k=b;
M[a].push(rt);
}
if(cmd=="OUT") {
int a; cin>>a;
if(M[a].empty())puts("EMPTY");
else {
R=M[a].top();M[a].pop();
cout<<R.num<<endl;
}
}
}
}
return 0;
}
#include<iostream>
#include<string>
#include<cstring>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<cctype>
#include<iomanip>
#include<queue>
using namespace std;
const int inf=10000000;
struct node {
int num;
int k;
bool operator < (const node& x) const {
if(k!=x.k)return k<x.k;
else return num>x.num;
}
}rt,R;
int main() {
int n;
while(cin>>n) {
priority_queue<node>M[4];
for(int i=1,t=1; i<=n; ++i) {
string cmd; cin>>cmd;
if(cmd=="IN") {
int a,b; cin>>a>>b;
rt.num=t++, rt.k=b;
M[a].push(rt);
}
if(cmd=="OUT") {
int a; cin>>a;
if(M[a].empty())puts("EMPTY");
else {
R=M[a].top();M[a].pop();
cout<<R.num<<endl;
}
}
}
}
return 0;
}