点击打开链接

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!

题意:

依据操作指令进行操作,也就是出队,还是入队。而在出队时。要注意。保证优先级低的先出来,要是优先级一样的话。就先入队的先出来。从题目就非常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) 优先队列的更多相关文章

  1. hdu 1509 Windows Message Queue (优先队列)

    Windows Message QueueTime Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Oth ...

  2. zoj 2724 Windows Message Queue 优先队列

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1724 题目大意: 给出两种操作,GET要求取出当前队首的元素,而PUT会输入名 ...

  3. Windows Message Queue(优先队列)

    欢迎参加——BestCoder周年纪念赛(高质量题目+多重奖励) Windows Message Queue Time Limit: 2000/1000 MS (Java/Others)    Mem ...

  4. hdoj 1509 Windows Message Queue【优先队列】

    Windows Message Queue Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Ot ...

  5. hdu 1509 Windows Message Queue

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1509 Windows Message Queue Description Message queue ...

  6. Windows Message Queue

    Windows Message Queue Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...

  7. zoj 2724 Windows Message Queue

    Windows Message Queue Time Limit: 2 Seconds      Memory Limit: 65536 KB Message queue is the basic f ...

  8. D - Windows Message Queue

    来源hdu1509 Message queue is the basic fundamental of windows system. For each process, the system mai ...

  9. HDU 1509 Windows Message Queue(队列)

    题目链接 Problem Description Message queue is the basic fundamental of windows system. For each process, ...

随机推荐

  1. Python 进阶 之 map()函数

    map()函数主要功能是对批量的元素进行相同的操作时可以大幅度的缩减代码. map函数参数详解: map(param1,param2,***) param1:函数名 ,要对批量元素进行处理的funct ...

  2. Chrome崩溃卡死

    Chrome崩溃卡死主要是Flash崩溃导致,解决办法:1,chrome://plugins,停用Chrome自带的Flash插件:2,停用GPU加速.chrome://flags,停用对所有网页执行 ...

  3. mvc路由问题

    概述 使用MVC Routing可以不必将URL映射到网站中特定文件.通过添加Routing规则,使URL能够安装指定的规则发送HTTP请求,这种方式能够简洁.方便.有效.快速的实现对指定页面的访问或 ...

  4. POJ 2387 链式前向星下的SPFA

    (POJ)[http://poj.org/problem?id=2387] Til the Cows Come Home Time Limit: 1000MS Memory Limit: 65536K ...

  5. Python与数据结构[4] -> 散列表[1] -> 分离链接法的 Python 实现

    分离链接法 / Separate Chain Hashing 前面完成了一个基本散列表的实现,但是还存在一个问题,当散列表插入元素冲突时,散列表将返回异常,这一问题的解决方式之一为使用链表进行元素的存 ...

  6. shell 查看 具体某行的值

    sed: sed '5!d' file awk: awk 'NR==5' file

  7. [BZOJ1143][CTSC2008]祭祀river(Dilworth定理+二分图匹配)

    题意:给你一张n个点的DAG,最大化选择的点数,是点之间两两不可达. 要从Dilworth定理说起. Dilworth定理是定义在偏序集上的,也可以从图论的角度解释.偏序集中两个元素能比较大小,则在图 ...

  8. python基础-协程函数、递归、模块、包等内容

    1. 协程函数 1.1 yield基本用法 yield作用 1.把函数的执行结果封装好,即封装__iter__和__next__,即得到一个迭代器 2.与return功能类似,都可以返回值,但不同的是 ...

  9. 将html文档转成pdf

    (1)使用场景:在项目中使用到了合同,只有在合同的头部,是不相同的.在合同的主体部分都是相同的,因此就把他放到了模板(html文件)里面. 在用户线上签约完成之后,可以将pdf版的合同下载. (2)需 ...

  10. java直接跳转页面

    public static String genForwardHtml(String url, Map<String, String> parameters, String charset ...