Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 4392 Accepted Submission(s): 1744
GET PUT msg1 10 5 PUT msg2 10 4 GET GET GET
EMPTY QUEUE! msg2 10 msg1 10 EMPTY QUEUE!
问题描述
消息队列是windows系统的基本(basic fundamental)。对于每个程序,系统都会有一个消息队列。如果这个程序被操作,比如鼠标点击,文本(text)修改,系统将会给队列发送消息。与此同时,在非空的情况下,程序将会根据优先级值(priority value)从队列中不断得到消息。注意,优先级值越低表示的优先级越高。在这个问题中,你要模仿接收消息和发送消息的队列。
输入
输入项中只有一种情况。每一行是一个指令,接受或发送,即接收消息和发送消息。如果指令是发送,则会有一个表示消息名称的字符串还有两个整数,一个表示参数(parameter),一个表示优先权(priority)最多会有60000条指令。注意,一条消息能够出现两次或者更多,如果两条消息具有同样的优先权,那么先接收的那条将被执行(IE,FIFO都是这样的)。直至文件结束。
输出
对于每条接受指令,从消息队列中输出一行消息名和参数。如果在队列中没有消息,那么输出空队列。对于接受指令则没有输出。
算是我做的第一道队列题吧,其实不难,主要练得就是输入输出和排序。不过要注意以下几点:
1.由于编译器等原因的不同,有的要加friend。
2.编号的时候直接++i就可以了,后面不用考虑想减了,不知道为什么不减就不错,可能i是用来统计输入的总和的吧。
#include<stdio.h> #include<queue> #include<string.h> using namespace std; struct node{ char name[110]; int num,id,no; friend bool operator < (node a,node b){ if(a.no!=b.no) return a.no>b.no; return a.id>b.id; } }; priority_queue <node> q; int main(){ char a[110]; int i=0; while(~scanf("%s",a)){ node temp; if(strcmp(a,"GET")==0){ if(!q.empty()) { printf("%s %d\n",q.top().name,q.top().num); q.pop(); } else{ printf("EMPTY QUEUE!\n"); } } else{ scanf("%s %d %d",temp.name,&temp.num,&temp.no); temp.id=++i; q.push(temp); } } return 0; }