hdu1509(Windows Message Queue) 優先隊列

點擊打開鏈接

Problem 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 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.
 

Input

There'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.
 

Output

For 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

GET PUT msg1 10 5 PUT msg2 10 4 GET GET GET
 

Sample Output

EMPTY QUEUE! msg2 10 msg1 10 EMPTY 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;
			}
		}
	}
}







發佈了148 篇原創文章 · 獲贊 29 · 訪問量 17萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章