优先队列——Windows Message Queue
2018-03-06 23:16
393 查看
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.InputThere'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.OutputFor 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!刚开始学习优先队列,只知道一些基本知识,后来看了大佬们的代码才发现优先队列和结构体结合还有这等妙用
#include<cstdio> #include<iostream> #include<algorithm> #include<queue> #include<cstring> using namespace std; struct node{ char name[10010]; int sign,id,vip; bool friend operator < (node a,node b) { if(a.vip==b.vip) return a.sign>b.sign;//用sign来记录存入队列的顺序 return a.vip>b.vip;//有小到大排列 } }temp; int main(){ char str[4]; //char string[10010]; int x=1; priority_queue<node> q; while(scanf("%s",str)!=EOF){ if(strcmp(str,"PUT")==0){ scanf("%s %d %d",temp.name,&temp.id,&temp.vip); temp.sign=x; x++;///通过x的递增来逐步把信息存入队列 q.push(temp); } if(strcmp(str,"GET")==0){ if(q.empty()) printf("EMPTY QUEUE!\n");//因为感叹号没加这道题错了好几次 else{ temp=q.top(); printf("%s %d\n",temp.name,temp.id); q.pop(); } } } return 0; }
相关文章推荐
- HDU - 1509 Windows Message Queue (优先队列 重载运算符)
- HDU1509 Windows Message Queue(优先队列priority_queue及重载运算符)
- hud 1509 Windows Message Queue 简单优先队列
- zoj 2724 Windows Message Queue 优先队列
- hdu1509 Windows Message Queue(优先队列水)
- HDU - 1509 Windows Message Queue (优先队列 重载运算符)
- zoj 2724 Windows Message Queue 优先队列
- HDU - 1509 Windows Message Queue (优先队列 重载运算符)
- HDU 1509:Windows Message Queue【优先队列】
- HDU - 1509 Windows Message Queue (优先队列 重载运算符)
- HDU - 1509 Windows Message Queue (优先队列 重载运算符)
- hdoj 1509 Windows Message Queue【优先队列】
- HDU - 1509 Windows Message Queue (优先队列 重载运算符)
- Hdoj 1509 -Windows Message Queue 优先队列最小堆实现
- Windows Message Queue(STL优先队列)
- HDU - 1509 Windows Message Queue (优先队列 重载运算符)
- Windows Message Queue(priority_queue优先队列)
- HDU - 1509 Windows Message Queue (优先队列 重载运算符)
- HDUoj 1509 Windows Message Queue ( 优先队列
- hdu 杭电1509 Windows Message Queue【优先队列】