hdu1509(Windows Message Queue) 优先队列
Problem Description
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
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
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) 优先队列的更多相关文章
- hdu 1509 Windows Message Queue (优先队列)
Windows Message QueueTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Oth ...
- zoj 2724 Windows Message Queue 优先队列
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1724 题目大意: 给出两种操作,GET要求取出当前队首的元素,而PUT会输入名 ...
- Windows Message Queue(优先队列)
欢迎参加——BestCoder周年纪念赛(高质量题目+多重奖励) Windows Message Queue Time Limit: 2000/1000 MS (Java/Others) Mem ...
- hdoj 1509 Windows Message Queue【优先队列】
Windows Message Queue Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Ot ...
- hdu 1509 Windows Message Queue
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1509 Windows Message Queue Description Message queue ...
- Windows Message Queue
Windows Message Queue Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- zoj 2724 Windows Message Queue
Windows Message Queue Time Limit: 2 Seconds Memory Limit: 65536 KB Message queue is the basic f ...
- D - Windows Message Queue
来源hdu1509 Message queue is the basic fundamental of windows system. For each process, the system mai ...
- HDU 1509 Windows Message Queue(队列)
题目链接 Problem Description Message queue is the basic fundamental of windows system. For each process, ...
随机推荐
- jQuery 特殊选择器this
特殊选择器this 相信很多刚接触jQuery的人,很多都会对$(this)和this的区别模糊不清,那么这两者有什么区别呢? this是JavaScript中的关键字,指的是当前的上下文对象,简单的 ...
- 天猫首页迷思之-jquery实现整个div的懒加载(2)-插件面向对象化-闭包和原型的实例
前文有简单的实现了一个制作懒加载的方法,但其实以方法的形式做插件扩展性不强.那么本文就来用面向对象的方法将其制作成一个真正的插件: 我想要的最终的调用效果是: $(".loading&quo ...
- AC日记——[SCOI2012]喵星球上的点名 bzoj 2754
2754 思路: AC自动机暴力处理匹配: 强大的ac自动机,强大的fail树,强大的map,强大的vector,强大的指针: 代码: #include <map> #include &l ...
- 【转】jmeter入门教程- Jmeter教程及技巧汇总
https://blog.csdn.net/zouxiongqqq/article/details/72843500
- 【转】jenkins插件pipeline使用介绍
摘要: pipeline字面意思就是流水线,将很多步骤按顺序排列好,做完一个执行下一个.下面简单介绍下如何使用该插件帮我们完成一些流水线型的任务 pipeline字面意思就是流水线,将很多步骤按顺序排 ...
- (12)python 标准库
模块 如果模块和自己写的程序不在同一个目录,可以通过sys.path.append(路径)把程序引入 import sys sys.path.append('C:/abc')#注意 \ 的方向 意思是 ...
- python3爬虫爬取网页思路及常见问题(原创)
学习爬虫有一段时间了,对遇到的一些问题进行一下总结. 爬虫流程可大致分为:请求网页(request),获取响应(response),解析(parse),保存(save). 下面分别说下这几个过程中可以 ...
- 洛谷——P1589 泥泞路
P1589 泥泞路 题目描述 暴雨过后,FJ的农场到镇上的公路上有一些泥泞路,他有若干块长度为L的木板可以铺在这些泥泞路上,问他至少需要多少块木板,才能把所有的泥泞路覆盖住. 输入输出格式 输入格式: ...
- volatile 和 Interlocked
class Volatile_Test3 { ; public static void Test() { count = ; Task[] tasks = ]; ; i < tasks.Leng ...
- 一种可以做app性能监控的app
http://easytest.taobao.com/?spm=0.0.0.0.ljgQHN