HDU1509 Windows Message Queue(优先队列priority_queue及重载运算符)

来源:互联网 发布:仿卷皮源码 编辑:程序博客网 时间:2024/05/22 07:03

题目:

Windows Message Queue

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 5763    Accepted Submission(s): 2324


Problem 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
GETPUT msg1 10 5PUT msg2 10 4GETGETGET
 

Sample Output
EMPTY QUEUE!msg2 10msg1 10EMPTY QUEUE!
 

Author
ZHOU, Ran
 
代码:
#include <stdio.h>#include <string.h>#include <math.h>#include <map>#include <stack>#include <queue>#include <vector>#include <algorithm>#define mem(a,b) memset(a,b,sizeof(a))using namespace std;struct node{    int str[100];//消息名称    int s1;//消息的参数    int s2;//消息的优先级(优先级数字越小,优先级越高)    int cnt;//记录消息放进去的顺序    friend bool operator < (node a, node b)//重载运算符    {        if(a.s2!=b.s2)//当优先级不等的时候            return a.s2>b.s2;//优先级高的先出队(从小到大)        else            return a.cnt>b.cnt;//优先级相同时,先进去的先出去    }};int main(){    int k=0;    char s[10];    priority_queue<node>q;//定义优先队列q    node now;    while(~scanf("%s",s))    {        if(strcmp(s,"GET")==0)        {            if(q.empty())            {                printf("EMPTY QUEUE!\n");                continue;            }            else            {                now=q.top();                q.pop();                printf("%s %d\n",now.str,now.s1);//输出消息和消息的参数            }        }        else if(strcmp(s,"PUT")==0)        {            scanf("%s%d%d",now.str,&now.s1,&now.s2);//输入消息和消息的参数以及优先级            now.cnt=k++;//用k来记录进去的先后顺序            q.push(now);//入队        }        mem(s,'\0');    }    return 0;}

//优先队列的简单应用

0 0