ZOJ 2724 Windows Message Queue

Source: Internet
Author: User

A-Windows Message Queue (10.2.1)) crawling in process ... crawling failed Time Limit: -Ms Memory Limit:65536Kb 64bit IO Format:%lld &%llu

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 would add a message to the queue. Meanwhile, the process would do a loop for getting message from the queue according to the priority value if it's not empt Y. Note the less priority value means the higher priority. In this problem, you is 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 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 both integer means the parameter and priority Follo Wed by. There'll is at the most 60000 command. Note that one message can appear twice or more and if the messages has the same priority, the one comes first would be pro Cessed first. (i.e., FIFO for the same priority.) Process to the End-of-file.

Output

For each "GET" command, the output of the command getting from the message queue with the name and parameter on one line. If There's no message in the queue, output "EMPTY queue!". There ' s no output for "PUT" command.

Sample Input

Getput MSG1 5PUT msg2 4GETGETGET

Sample Output

EMPTY queue!msg2 10MSG1 10EMPTY queue!


#include <iostream> #include <cstring>using namespace std; #define MAXN 60005#define maxs 101int top;//heap Length int used;//buffer pointer int heap[maxn];//heap struct mes{char name[maxs];//name int para;//parameter int pri;//precedence int t;//order}P[MAXN];    int compare (int a,int b)//priority is the first keyword {if (p[a].pri<p[b].pri) return-1;    if (P[A].PRI&GT;P[B].PRI) return 1;    if (p[a].t<p[a].t) return-1;    if (p[a].t>p[b].t) return 1; return 0;}    void swap (int &a,int &b) {int temp;    Temp=a;    A=b; B=temp;}    int main () {top=0;    Used=0;    int cnt=0;//The order of the messages logged in string str;                while (CIN&GT;&GT;STR) {if (str== "GET") {if (top)//Joshui is not empty, the first name and parameter of the output heap message {                cout<<p[heap[1]].name<< "" <<p[heap[1]].para<<endl;                int k;                k=1;//heap Tail node modulation heap first heap[k]=heap[top--];//heap length -1 while (k*2<=top)//Reduce the first node of the heap to a suitable position {int T t=k*2;//calculates the node if (t<top&& (compare (Heap[t+1],heap[t]) <0) that the right and left son has the lowest weight) + +                    T if (compare (Heap[t],heap[k]) <0)//node T has a lower weight than its parent, then swap and continue down {swap (heap[t],heap[k])                        ;                    k=t;                } else break; }} else {cout<< "EMPTY queue!"            <<endl;            }} else {cin>>p[used].name>>p[used].para>>p[used].pri;            p[used].t=cnt++;//Order +1 int k=++top;//The message into the heap tail heap[k]=used++; while (k>1)//Bottom-up, raise the heap tail node to the appropriate position {int t=k/2;//calculates the parent of the K ordinal T if (compare (heap[t],heap [K])                    &GT;0)//If the weight of the node T is greater than the weight of K, then the Exchange node T and Node K {swap (heap[t],heap[k]);                k=t;           }     else break; }}} return 0;}






Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.