標籤:ace art 入隊 sse [] oid queue類 pen strong
點擊開啟連結
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!
題意:
依據操作指令進行操作,也就是出隊,還是入隊。而在出隊時。要注意。保證優先順序低的先出來,要是優先順序一樣的話。就先入隊的先出來。從題目就非常easy看出要用優先隊列做,恰好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) 優先隊列