ZOJ 2724 Windows Message Queue(二叉堆||优先队列)
2015-10-27 19:42
573 查看
Windows Message Queue
Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld
& %llu
Submit Status Practice ZOJ
2724
Appoint description:
System Crawler (2015-10-27)
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
Sample Output
题意: 给入输入输出命令,输入的信息包括 姓名(name) ,数字(para) 和权值(pri)其中 权值小的优先;
分析:构造小根二叉堆,利用小根二叉堆就能解决,这一题还一个条件说是权值相同的先出现的优先,在比较函数中还加了两个条件来判断。
二叉堆代码:50ms
优先队列:60ms
Windows Message Queue
Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld
& %llu
Submit Status Practice ZOJ
2724
Appoint description:
System Crawler (2015-10-27)
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!
题意: 给入输入输出命令,输入的信息包括 姓名(name) ,数字(para) 和权值(pri)其中 权值小的优先;
分析:构造小根二叉堆,利用小根二叉堆就能解决,这一题还一个条件说是权值相同的先出现的优先,在比较函数中还加了两个条件来判断。
二叉堆代码:50ms
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int MAX = 60000 + 10; const int MAXS = 100; typedef struct info { char name[MAXS]; int para; int pri; int t; }info; info p[MAX]; int heap[MAX]; int top,used; int main() { used = 0; top = 0; int cnt = 0; char s[MAXS]; while(scanf("%s", s) != EOF) { if(strcmp(s,"GET") == 0) { if(top == 0) printf("EMPTY QUEUE!\n"); else { printf("%s %d\n",p[heap[1]].name, p[heap[1]].para); int k = 1; heap[k] = heap[top--]; while(k*2 <= top) { int son = k *2 ; if(son < top && p[heap[son + 1]].pri < p[heap[son]].pri) { son++; } if(p[heap[son]].pri < p[heap[k]].pri) { swap(heap[son],heap[k]); k = son; } else break; } } } else if(strcmp(s,"PUT") == 0) { scanf("%s%d%d", p[used].name, &p[used].para, &p[used].pri); p[used].t = cnt++; int k = ++top; heap[k] = used++; while(k > 1) { int f = k / 2; if(p[heap[f]].pri > p[heap[k]].pri) { swap(heap[f], heap[k]); k = f; } else break; } } } return 0; }
优先队列:60ms
#include <cstdio> #include <cstdlib> #include <iostream> #include <string.h> #include <queue> #include <vector> #include <limits.h> using namespace std; const int MAX = 60010; const int MAXS = 50; struct info { char name[MAXS]; int para; int pri; int t; }; info p[MAX]; char s[MAX]; bool operator< (info a,info b) { if(a.pri == b.pri) return a.t > b.t; return a.pri > b.pri; } priority_queue<info> q; int main() { while(q.size()) q.pop(); int ind = 0; while(scanf("%s", s) != EOF) { if(strcmp(s,"PUT") == 0) { scanf("%s %d %d", p[ind].name, &p[ind].para, &p[ind].pri); p[ind].t = ind; q.push(p[ind]); ind ++; } else if(strcmp(s,"GET") == 0) { if(q.size()) { info temp = q.top(); q.pop(); printf("%s %d\n", temp.name,temp.para); } else printf("EMPTY QUEUE!\n"); } } return 0; }
相关文章推荐
- UE4 Plugins
- UIButton的一些属性和方法
- UIscrollView的属性及其协议方法
- PHP实现quicksort
- UItextfield的一些方法总结
- javax.persistence.TransactionRequiredException: Executing an update/delete query
- QtQuick手机滑动界面Demo
- Easyui 定制window窗口
- android6.0 蓝牙搜索不到设备 BluetoothAdapter.getDefaultAdapter().startDiscovery()不起作用
- IOS——UITextField自动适应键盘弹出
- IOS——UITextField自动适应键盘弹出
- iOS 【UIKit-NSTimer 的创建及运行循环模式】
- UIImageView变灰
- C#学习(6):DataSet构建EasyUI树结构
- request.getSession(false)
- pat 1085 Perfect Sequence
- UI标记页面效果图
- 将十六进制的字符串转化为UIImage
- UIImageView加载图片的两种方式
- ui切图命名规则