zju2724

Windows Message Queue

http://acm.zju.edu.cn/show_problem.php?pid=2724

Time limit: 1 Seconds

Memory limit: 32768K

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.

1. 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.

2. 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.

3. Sample Input

GET
PUT msg1 10 5
PUT msg2 10 4
GET
GET
GET

4. Sample Output

EMPTY QUEUE!
msg2 10
msg1 10
EMPTY QUEUE!

Author: ZHOU, Ran

Problem Source: Zhejiang University Local Contest 2006, Preliminary


   1 /*Written by czk*/
   2 #include <cstdio>
   3 #include <algorithm>
   4 #include <functional>
   5 using namespace std;
   6 
   7 struct message {
   8     char name[20];
   9     int parameter;
  10     int priority;
  11     int serial;
  12     bool operator>(const message &m) const {
  13         return priority > m.priority || priority == m.priority && serial > m.serial;
  14     }
  15 };
  16 
  17 int main() {
  18     message q[60000];
  19     int serial = 0;
  20     int size = 0;
  21     make_heap(q, q, greater<message>());
  22     char command[10];
  23     while(scanf("%s", command)!=EOF) {
  24         if(command[0] == 'G') {
  25             if(size==0)
  26                 puts("EMPTY QUEUE!");
  27             else {
  28                 pop_heap(q, q+size,greater<message>());
  29                 size--;
  30                 printf("%s %d\n", q[size].name, q[size].parameter);
  31             }
  32         }
  33         else {
  34             scanf("%s%d%d", q[size].name, &q[size].parameter, &q[size].priority);
  35             q[size].serial = serial++; 
  36             size++;
  37             push_heap(q, q+size, greater<message>());
  38         }
  39     }
  40 }

   1 /*
   2 Another version using priority_queue
   3 Written by czk
   4 */
   5 #include <cstdio>
   6 #include <algorithm>
   7 #include <functional>
   8 #include <queue>
   9 using namespace std;
  10 
  11 struct message {
  12     char name[20];
  13     int parameter;
  14     int priority;
  15     int serial;
  16     message(char *n, int p, int pri, int s):parameter(p), priority(pri), serial(s) 
  17     {
  18         strcpy(name, n);
  19     }
  20 };
  21 
  22 bool great(const message *m1, const message *m2) 
  23 {
  24     return m1->priority > m2->priority || m1->priority == m2->priority && m1->serial > m2->serial;
  25 }
  26 
  27 int main() {
  28     priority_queue<message *, vector<message *>, bool(*)(const message *, const message *) > q(&great);
  29     int serial = 0;
  30     char command[10];
  31     while(scanf("%s", command)!=EOF) {
  32         if(command[0] == 'G') {
  33             if(q.empty())
  34                 puts("EMPTY QUEUE!");
  35             else {
  36                 message *p = q.top();
  37                 printf("%s %d\n",p->name, p->parameter);
  38                 delete p;
  39                 q.pop();
  40             }
  41         }
  42         else {
  43             char name[20];
  44             int parameter, priority;
  45             scanf("%s%d%d", name, &parameter, &priority);
  46             q.push(new message(name, parameter, priority, serial++));
  47         }
  48     }
  49 }

zju2724 (2008-02-23 15:35:19由localhost编辑)