题意:
思路: 用优先队列直接模拟就OK了,另外优先队列存pair的时候比较的是first的值,实测!!
上代码:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <iostream> 2 #include <queue> 3 #include <cstdio> 4 #include <algorithm> 5 #include <cmath> 6 #include <cstring> 7 #include <queue> 8 #include <vector> 9 #define INF 0x3f3f3f3f 10 #define FRE() freopen("in.txt","r",stdin) 11 12 using namespace std; 13 typedef long long ll; 14 typedef pair<int,string> P; 15 const int maxn = 1e5+10; 16 priority_queue<P, vector<P>, greater<P> > que; 17 string op,name; 18 int n,id; 19 20 int main() { 21 //FRE(); 22 cin>>n; 23 for(int i = 0; i<n; i++) { 24 cin>>op; 25 if(op[0]=='P') { 26 cin>>name>>id; 27 que.push(P(id, name)); 28 } else { 29 if(que.empty()){ 30 cout<<"EMPTY QUEUE!"<<endl; 31 } 32 else{ 33 P p = que.top(); 34 que.pop(); 35 cout<<p.second<<endl; 36 } 37 } 38 } 39 return 0; 40 } 41 /* 42 样例输入: 43 9 44 PUT msg1 5 45 PUT msg2 4 46 GET 47 PUT msg3 2 48 PUT msg4 4 49 GET 50 GET 51 GET 52 GET 53 样例输出: 54 msg2 55 msg3 56 msg4 57 msg1 58 EMPTY QUEUE! 59 */