天天看点

PTA 7-7 Windows消息队列

消息队列是Windows系统的基础。对于每个进程,系统维护一个消息队列。如果在进程中有特定事件发生,如点击鼠标、文字改变等,系统将把这个消息加到队列当中。同时,如果队列不是空的,这一进程循环地从队列中按照优先级获取消息。请注意优先级值低意味着优先级高。请编辑程序模拟消息队列,将消息加到队列中以及从队列中获取消息。

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <queue>
using namespace std;
const int maxn = 1e5 + 10;
struct Data {
    int x, id;
    Data(int xx, int i) : x(xx), id(i) {}
    bool operator < (const Data &temp) const {
        return x > temp.x;
    }
};
char s[20], str[maxn][20];
int main() {
    int n, x, cnt = 0;
    priority_queue<Data> q;
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) {
        scanf("%s", s);
        if (s[0] == 'P') {
            scanf("%s%d", str[++cnt], &x);
            q.push(Data(x, cnt));
        }
        else {
            if (q.empty()) printf("EMPTY QUEUE!\n");
            else {
                Data d = q.top(); q.pop();
                printf("%s\n", str[d.id]);
            }
        }
    }
    return 0;
}