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, ...
随机推荐
- ros bashrc 无法source setup.sh
不知道什么时候开始的,莫名其妙首先是QT闪退,无法找到头文件,然后命令行进去catkin无法提示catkin_make 手动source之后就好了 问题出现在bashrc中source失败了 所以检查 ...
- Spring ClassPathXmlApplicationContext和FileSystemXmlApplicationContext
先说:ClassPathXmlApplicationContext 这个类,默认获取的是WEB-INF/classes/下的路径,也就是在myeclipse的src下的路径,所以用这个是获取不到WEB ...
- React Native - 0序言
1. 什么是React Native? React Native是Facebook在React.js Conf 2015大会上推出的一个用于开发Android t iOS App的一个框架.主要编程语 ...
- Linux下Tomcat安装配置
买了台阿里云服务器,因为配置比较低,所以用Linux系统,这里记录一下我在Linux系统中Tomcat的安装配置. 前提JDK已经安装好. 安装 首先在/usr/local/下建立一个tomcat的文 ...
- 7/25 CSU-ACM2018暑假集训比赛1
题目链接 [A - Tricky Sum ] In this problem you are to calculate the sum of all integers from 1 to n, but ...
- HDU 2829 Lawrence
$dp$,斜率优化. 设$dp[i][j]$表示前$i$个数字切了$j$次的最小代价.$dp[i][j]=dp[k][j-1]+p[k+1][i]$.观察状态转移方程,可以发现是一列一列推导出来的.可 ...
- HDU 2256 Problem of Precision (矩阵快速幂)(推算)
Problem of Precision Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
- rsync用于同步目录
rsync是unix/linux下同步文件的一个高效算法,它能同步更新两处计算机的文件与目录,并适当利用查找文件中的不同块以减少数据传输.rsync中一项与其他大部分类似程序或协定中所未见的重要特性是 ...
- 小程序使用npm模块(引入第三方UI),报错的多种解决办法。
前言引入第三方模块时,我遇到了很多坑. 首先是微信.第三方模块的文档描述不清楚.其次.搜索到的博客,大部分是抄的文档 / 相互转载抄袭.作用有限. 于是,我自己做了各种条件下的测试.解决各种情况的引入 ...
- [UOJ206]Gap
子任务$1$:直接找到最大值后从两边开始找,一步一步从左右到中间确定所有数,调用次数是$\left\lceil\dfrac n2\right\rceil$ 子任务$2$:先找到最大值$mx$和最小值$ ...