Description
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.
Input
There's only one test case in the input. Each line is a command, "GET" or "PUT", which means getting message or putting message. If the command is "PUT", there're one string means the message name and two integer means the parameter and priority followed by. There will be at most 60000 command. Note that one message can appear twice or more and if two messages have the same priority, the one comes first will be processed first.(i.e., FIFO for the same priority.) Process to the end-of-file.
Output
For each "GET" command, output the command getting from the message queue with the name and parameter in one line. If there's no message in the queue, output "EMPTY QUEUE!". There's no output for "PUT" command.
Sample Input
GET PUT msg1 10 5 PUT msg2 10 4 GET GET GETSample Output
EMPTY QUEUE! msg2 10 msg1 10 EMPTY QUEUE!解法一:本题是典型的二叉堆题,又因为优先级值越低意味着优先级高,就可以用最小堆来处理。对于指令CET就相当与,删除最小堆顶成元素,PUT指令就相当于往最小堆里插入元素。
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn=60000+10;
int heap[maxn];
struct node
{
char name[200];
int para,pri,cnt;
};
node p[maxn];
int compare(int a,int b)
{
if(p[a].pri<p[b].pri)
return -1;
if(p[a].pri>p[b].pri)
return 1;
if(p[a].cnt<p[b].cnt)
return -1;
if(p[a].cnt>p[b].cnt)
return 1;
return 0;
}
void swap(int &a,int &b)
{
int temp;
temp=a;
a=b;
b=temp;
}
int main()
{
char s[10];
int top,cnt1,used;
top=0;used=0;cnt1=0;
while(scanf("%s",s)!=EOF)
{
if(strcmp(s,"GET")==0)
{
if(top)
{
cout<<p[heap[1]].name<<' '<<p[heap[1]].para<<endl;
int k=1;
heap[k]=heap[top--];
while(2*k<=top)
{
int t=2*k;
if(t<top&&(compare(heap[t+1],heap[t])<0))
++t;
if(compare(heap[t],heap[k])<0)
{
swap(heap[t],heap[k]);
k=t;
}
else
break;
}
}
else
cout<<"EMPTY QUEUE!"<<endl;
}
else
{
cin>>p[used].name>>p[used].para>>p[used].pri;
p[used].cnt=cnt1++;
int k=++top;
heap[k]=used++;
while(k>1)
{
int t=k/2;
if(compare(heap[t],heap[k])>0)
{
swap(heap[t],heap[k]);
k=t;
}
else
break;
}
}
}
return 0;
}
法二:利用优先队列
#include <iostream>
#include <cstdio>
#include <queue>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn=60000+10;
int heap[maxn];
struct node
{
char name[200];
int para,pri;
}p;
bool operator<(const node &a,const node &b)
{
if(a.pri>b.pri)
return 1;
else
return 0;
}
char s[200];
int main()
{
priority_queue<node>Q;
while(scanf("%s",s)!=EOF)
{
if(strcmp(s,"GET")==0)
{
if(Q.empty())
printf("EMPTY QUEUE!\n");
else
{
printf("%s %d\n",Q.top().name,Q.top().para);
Q.pop();
}
}
else
{
scanf("%s%d%d",p.name,&p.para,&p.pri);
Q.push(p);
}
}
return 0;
}