消息队列 | ||||||
| ||||||
Description | ||||||
Windows操作系统是基于消息的,也就是说任何的事件,包括鼠标移动和点击,键盘的输入,都会被放入操作系统的消息队列中,而消息本身带有一定的参数和优先级。Windows会优先处理优先级较高的消息,当两个消息优先级相同时,按照先来先服务的原则进行处理,你的任务就是模拟这种机制。 | ||||||
Input | ||||||
输入首先分为两种,GET表示从消息队列中取出一个消息。PUT表示把一个消息放入消息队列,每一个消息包含三个部分:内容,参数和优先级(数字越小优先级越高)。输入亦按照此顺序进行。消息的内容长度不会超过15。最多不超过60000个操作。 | ||||||
Output | ||||||
对于每一个GET操作,若队列为空,输出EMPTY QUEUE!否则输出按照规则得到的第一个消息的内容和参数。 | ||||||
Sample Input | ||||||
GET PUT msgone 11 6 PUT msgtwo 8 4 GET GET GET | ||||||
Sample Output | ||||||
EMPTY QUEUE! msgtwo 8 msgone 11 EMPTY QUEUE! |
#include<bits/stdc++.h>
using namespace std;
struct node
{
char s[50];
int a, b;
int num;
bool operator < (const node &x) const
{
if(b == x.b)
return num > x.num;
return b > x.b;
}
} op;
int main()
{
char a[50];
priority_queue <node> q;
int num = 0;
while(scanf("%s", a) != EOF)
{
if(a[0] == 'G')
{
if(!q.size())
puts("EMPTY QUEUE!");
else
{
printf("%s %d\n", q.top().s, q.top().a);
q.pop();
}
}
if(a[0] == 'P')
{
scanf("%s%d%d", op.s, &op.a, &op.b);
op.num = num++;
q.push(op);
}
}
return 0;
}