hdu1509(Windows Message Queue) 優先隊列

來源:互聯網
上載者:User

標籤: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) 優先隊列

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.