• HDU 1509 Windows Message Queue


                Windows Message Queue



     

    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
    GET
    PUT msg1 10 5
    PUT msg2 10 4
    GET
    GET
    GET
     

     

    Sample Output
    EMPTY QUEUE!
    msg2 10
    msg1 10
    EMPTY QUEUE!
     
    FIFO for the same priority.
     
     1 #include<cstdio>
     2 #include<queue> 
     3 #include<cstring>
     4 #include<algorithm>
     5 using namespace std;
     6 
     7 struct Node
     8 {
     9     char name[10];
    10     int parameter;
    11     int priority;
    12     int order;
    13     bool operator<(const Node &a)const
    14     { 
    15         if(priority==a.priority)
    16         return order>a.order;
    17         return priority>a.priority;
    18     }
    19 };
    20 
    21 int main()
    22 {
    23     //freopen("in.txt","r",stdin);
    24     int order;
    25     char command[5];
    26     priority_queue<Node>q;
    27     order=0;
    28     while(scanf("%s",command)!=EOF)
    29     {
    30         if(strcmp(command,"GET")==0)
    31         {
    32             if(!q.empty())
    33             {
    34                 Node temp=q.top();
    35                 q.pop();
    36                 printf("%s %d
    ",temp.name,temp.parameter); 
    37             }
    38             else
    39             printf("EMPTY QUEUE!
    ");
    40         }
    41         else
    42         {
    43             Node temp;
    44             scanf("%s%d%d",temp.name,&temp.parameter,&temp.priority);
    45             temp.order=++order;
    46             q.push(temp);
    47         }
    48     }
    49     return 0;
    50 }
  • 相关阅读:
    PostgreSQL 的 pl/pgsql 的 cannot begin/end transactions in PL/pgSQL错误
    PostgreSQL在何处处理 sql查询之五十三
    PostgreSQL在何处处理 sql查询之五十四
    PostgreSQL的CTE
    PostgreSQL在何处处理 sql查询之五十二
    PostgreSQL 语法树分析之前需要了解到知识
    PostgreSQL在何处处理 sql查询之五十一
    PostgreSQL在何处处理 sql查询之五十六
    PostgreSQL在何处处理 sql查询之五十五
    PostgreSQL在何处处理 sql查询之五十七
  • 原文地址:https://www.cnblogs.com/homura/p/4682776.html
Copyright © 2020-2023  润新知