標籤:hdu1509 優先隊列
點擊開啟連結
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 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.
InputThere‘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.
OutputFor 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!
題意:
根據操作指令進行操作,也就是出隊,還是入隊。而在出隊時,要注意,保證優先順序低的先出來,要是優先順序一樣的話,就先入隊的先出來。從題目就很容易看出要用優先隊列做,恰好java包中有個PriorityQueue類,就是現成的優先隊列。
解題過程:首先根據題意,可以建一個Message類,這裡麵包括mesg的一些資訊,主要包括,優先順序和進隊的序號(這個序號必須要,也就是第幾個進隊的,後面就知道了)。然後根據指令一步一步的操作,就可以得到答案。
注意:
1、PriorityQueue類與普通隊列最主要的區別就是多了個比較子。一般情況下,都是自己通過實現Comparator介面寫一個比較子,在new 優先隊列時將這個比較子丟進去就ok了,
構造方法中就有 PriorityQueue(int initialCapacity,Comparator<? super E> comparator)
使用指定的初始容量建立一個 PriorityQueue,並根據指定的比較子對元素進行排序。
2、雖然優先隊列在放入元素時,會通過其中的比較子進行比較後,放到相應的位置。但是至於它內部是怎麼比較的,怎麼放的,我還沒去深究,但是在這裡我知道,雖然題目是說先通過優先順序進行存放,然後通過進隊順序存放,聽起來只要考慮
優先順序排序就行了,主觀上就會以為只要優先順序相同時,也就是compare返回0,它就會放在同一優先順序,但先進來的元素後面,其實不然,並不是這樣的,這裡必須要通過進隊的序號來比較後才能達到想要的目的。
代碼實現:
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());//優先隊列Message messg;//messg類int count=0;while(sc.hasNext()){String operate=sc.next();if(operate.charAt(0)=='G'){//出隊if(priorityQue.isEmpty()){System.out.println("EMPTY QUEUE!");}else{System.out.println(priorityQue.poll());}}else{//入隊messg=new Message(sc.next(), sc.nextInt(), sc.nextInt(),count++);priorityQue.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{//這裡如果沒有id的比較,那順序就會出錯if(m1.id<m2.id){return -1;}else if(m1.id>m2.id){return 1;}else{return 0;}}}}
著作權聲明:本文為博主原創文章,未經博主允許不得轉載。
hdu1509(Windows Message Queue) 優先隊列