Message queue is the basic fundamental of windows system. For each process, the system maintains a message queue. If something happens to this process, such as mouse click, text change, the system will add a message to the queue. Meanwhile, the process will do a loop for getting message from the queue according to the priority value if it is not empty. Note that the less priority value means the higher priority. In this problem, you are asked to simulate the message queue for putting messages to and getting message from the message queue.
GET
PUT msg1 10 5
PUT msg2 10 4
GET
GET
GET
EMPTY QUEUE!
msg2 10
msg1 10
EMPTY QUEUE!
思路:按优先队列写一下就好了:
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<queue>
using namespace std;
struct node {
char name[50]; int x,y,t;
bool operator < (const node &a) const
{
if(a.t!=t)
return a.t<t;
return a.y<y;
}
}s;
char p[10];
int main()
{
priority_queue<node> que;
while(!que.empty() ) que.pop();
memset(p,0,sizeof(p));
int k=1;
while(scanf("%s",p)!=EOF)
{
memset(s.name,0,sizeof(s.name));
if(!strcmp(p,"PUT"))
{
scanf("%s %d %d",s.name ,&s.x ,&s.t );
s.y=k; que.push(s); k++;
}
else
{
if(!que.empty() )
{
printf("%s %d\n",que.top().name,que.top().x);
que.pop();
}
else puts("EMPTY QUEUE!");
}
}
return 0;
}