1. 程式人生 > >7-20 Windows訊息佇列 (25 分)(模擬水題)

7-20 Windows訊息佇列 (25 分)(模擬水題)

題意: 

思路: 用優先佇列直接模擬就OK了,另外優先佇列存pair的時候比較的是first的值,實測!!

上程式碼:

#include <iostream>
#include <queue>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <queue>
#include <vector>
#define INF 0x3f3f3f3f
#define FRE() freopen("in.txt","r",stdin)

using namespace std;
typedef long long ll;
typedef pair<int,string> P;
const int maxn = 1e5+10;
priority_queue<P, vector<P>, greater<P> > que;
string op,name;
int n,id;

int main() {
    //FRE();
    cin>>n;
    for(int i = 0; i<n; i++) {
        cin>>op;
        if(op[0]=='P') {
            cin>>name>>id;
            que.push(P(id, name));
        } else {
            if(que.empty()){
                cout<<"EMPTY QUEUE!"<<endl;
            }
            else{
                P p = que.top();
                que.pop();
                cout<<p.second<<endl;
            }
        }
    }
    return 0;
}
/*
樣例輸入:
9
PUT msg1 5
PUT msg2 4
GET
PUT msg3 2
PUT msg4 4
GET
GET
GET
GET
樣例輸出:
msg2
msg3
msg4
msg1
EMPTY QUEUE!
*/