消息队列 | ||||||
|
||||||
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! |
扫描二维码关注公众号,回复:
5857472 查看本文章
#include<string.h>
#include<functional>
#include<iostream>
using namespace std;
struct Message
{
string s;
int b;
int c;
int no;
friend bool operator<(Message m1, Message m2)
{
if(m1.c!=m2.c)
return m1.c > m2.c;
else
return m1.no>m2.no;
}
};
int main()
{
string op,str;
int x,y,num=0;
struct Message m;
priority_queue<Message>q;
while (!q.empty())
q.pop();
while (cin >> op)
{
if (op == "GET")
{
if (q.empty())
cout << "EMPTY QUEUE!" << endl;
else
{
m = q.top();
q.pop();
cout << m.s << " " << m.b << endl;
}
}
if (op == "PUT")
{
cin >> str >>x >> y;
m={str,x,y,num++};
q.push(m);
}
}
}