hdu1509 (Windows Message queue) Priority queue

Source: Internet
Author: User

Click to open link

Problem Descriptionmessage 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.
Inputthere ' 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.
Outputfor 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!

Test instructions

Operate according to the operation instructions, that is, the team, or the queue. And in the team, should pay attention to ensure that the priority is low first out, if the priority is the same, first out of the queue. From the topic it is easy to see the priority queue to do, just the Java package has a Priorityqueue class, is a ready-made priority queue.

Problem-solving process: First, according to test instructions, you can build a message class, which includes some information about MESG, including, priority and the serial number of the incoming team (this serial number must be, that is, the first few into the team, the following will know). Then, according to the instructions step-by-step operation, you can get the answer.

Attention:

1, Priorityqueue class and the general queue the main difference is that more than a comparator. In general, it is their own implementation of the comparator interface to write a comparator, in the new priority queue when the comparator is thrown in the OK,

In the construction method, there PriorityQueue(int initialCapacity,Comparator<? super E> comparator) is

creates one with the specified initial capacity PriorityQueue and sorts the elements according to the specified comparer.

2, although the priority queue is placed in the element, the comparator will be compared to the corresponding position. But as to how it is compared internally, how to put, I have not gone to the drill, but here I know, although the title is to say first through the priority of storage, and then through the queue in order to store, it sounds as long as consider

Priority sorting on the line, the subjective will assume that as long as the priority phase, that is, compare return 0, it will be placed in the same priority, but the advanced elements behind, it is not, it is not so, here must be in the serial number to compare to achieve the desired purpose.

Code implementation:

Import Java.util.comparator;import Java.util.priorityqueue;import Java.util.scanner;public class P1509 {public static void Main (string[] args) {Scanner sc=new Scanner (system.in); Priorityqueue<message> priorityque=new priorityqueue<message> (6000,new MesCmp ());//priority Queue Message messg;/ /MESSG class int Count=0;while (Sc.hasnext ()) {String operate=sc.next (); if (Operate.charat (0) = = ' G ') {//Outbound if ( Priorityque.isempty ()) {System.out.println ("EMPTY queue!");} Else{system.out.println (Priorityque.poll ());}} else{//Queue messg=new Message (Sc.next (), Sc.nextint (), Sc.nextint (), count++);p Riorityque.add (MESSG);}}} Class message{string Name;int value;int priority;int id;public Message (String name, int value, int priority,int ID) {this. Name = Name;this.value = Value;this.priority = Priority;this.id = ID;} Public String toString () {return name + "" + Value;}} Class MESCMP implements Comparator<message>{public int Compare (message m1, message m2) {if (m1.priority< m2.priority) {return-1;} else if (m1.priority> m2.priority) {return 1;} else{//here if there is no comparison of the ID, then the order will be wrong if (m1.id<m2.id) {return-1;} else if (m1.id>m2.id) {return 1;} Else{return 0;}}}}







Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

hdu1509 (Windows Message queue) Priority queue

Related Article

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.