【优先队列】HDU1509Windows Message Queue
2016-03-22 18:33
369 查看
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!
题目意思:
根据操作指令进行操作,也就是出队,还是入队。而在出队时,要注意,保证优先级低的先出来,要是优先级一样的话,就先入队的先出来。从题目就很容易看出要用优先队列做,
代码:
#include<iostream>
#include<queue>
using namespace std;
struct node{
string name;
int p; // parameter;
int c; // priority;
int id;
friend bool operator < (node a,node b){
if(a.c!=b.c) return a.c>b.c;
else return a.id>b.id; // FIFO;
}
}stu;
int main()
{
string s;
priority_queue<node>q;
int cnt=0;
while(cin>>s){
if(s[0]=='G'){
if(q.empty()){
cout<<"EMPTY QUEUE!"<<endl;
}else{
stu=q.top();
q.pop();
cout<<stu.name<<' '<<stu.p<<endl;
}
}else{
cin>>stu.name>>stu.p>>stu.c;
stu.id=cnt++;
q.push(stu);
}
}
return 0;
}
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!
题目意思:
根据操作指令进行操作,也就是出队,还是入队。而在出队时,要注意,保证优先级低的先出来,要是优先级一样的话,就先入队的先出来。从题目就很容易看出要用优先队列做,
代码:
#include<iostream>
#include<queue>
using namespace std;
struct node{
string name;
int p; // parameter;
int c; // priority;
int id;
friend bool operator < (node a,node b){
if(a.c!=b.c) return a.c>b.c;
else return a.id>b.id; // FIFO;
}
}stu;
int main()
{
string s;
priority_queue<node>q;
int cnt=0;
while(cin>>s){
if(s[0]=='G'){
if(q.empty()){
cout<<"EMPTY QUEUE!"<<endl;
}else{
stu=q.top();
q.pop();
cout<<stu.name<<' '<<stu.p<<endl;
}
}else{
cin>>stu.name>>stu.p>>stu.c;
stu.id=cnt++;
q.push(stu);
}
}
return 0;
}
相关文章推荐
- java中queue的使用
- iOS开发—UILable详解
- UESTC 1271 Search gold (DP,多校联盟3)
- 【转】Android UI开发第三十一篇——Android的Holo Theme
- android SpannableStringBuilder
- GUID(全局唯一标识符)
- IT十八掌作业_java基础第十六天_GUI/socket
- .NET跨平台之Sake和KoreBuild
- ios8 UITableView设置 setSeparatorInset:UIEdgeInsetsZero不起作用的解决办法
- 探究requestDisallowInterceptTouchEvent失效的原因
- A - Farey Sequence——(筛法求欧拉函数)
- 【转】Android UI开发第二十四篇——Action Bar
- 在AI中如何合理的处理图像{太原UI设计师}讲
- FineUI Gird单元格编辑 保存修改的数据到数据库 LINQ
- 扩展easyui 的表单验证
- AudioQueueStart Fail问题
- Android4.42-Settings源码分析之蓝牙模块Bluetooth(上)
- hihoCoder挑战赛19 A Rikka with Sequence
- 菜鸟成长记-UIPickerView使用
- web.xml is missing and <failOnMissingWebXml> is set to true