ZOJ 2724 Windows Message Queue(二叉堆||优先队列)
来源:互联网 发布:关于甜点的日剧 知乎 编辑:程序博客网 时间:2024/06/05 05: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
GETPUT msg1 10 5PUT msg2 10 4GETGETGET
Sample Output
EMPTY QUEUE!msg2 10msg1 10EMPTY 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;}
- zoj-2724 Windows Message Queue(二叉堆&&优先队列)
- ZOJ 2724 Windows Message Queue (二叉堆,优先队列)
- ZOJ 2724 Windows Message Queue(二叉堆||优先队列)
- ZOJ 2724 Windows Message Queue(二叉堆or优先队列)
- (二叉堆10.2.1)ZOJ 2724 Windows Message Queue(对结构体使用优先队列)
- zoj 2724 Windows Message Queue 优先队列
- zoj 2714 Windows Message Queue(优先队列、简单的二叉堆处理)
- ZOJ 2724/HDU 1509 Windows Message Queue(优先队列)
- ZOJ 2724 Windows Message Queue 堆
- Windows Message Queue(priority_queue优先队列)
- hdoj1509 Windows Message Queue (优先队列)
- Windows Message Queue(优先队列)
- hdu1509(Windows Message Queue) 优先队列
- Windows Message Queue(优先队列)
- Hdoj 1509 -Windows Message Queue 优先队列最小堆实现
- Windows Message Queue(杭电oj1509)(优先队列)
- HDOJ 1509 Windows Message Queue (优先队列)
- HDU1509题Windows Message Queue(优先队列)
- httpclient4.3设置代理请求
- android 屏幕适配的问题
- 微信公众平台API的Java通讯实现
- CSS杂记
- HDOJ 4771 Stealing Harry Potter's Precious(bfs + 状态压缩)
- ZOJ 2724 Windows Message Queue(二叉堆||优先队列)
- IO数据流总结(7)
- 高斯混合模型(理论部分)
- 同余问题(3)一元线性同余方程组
- 浅谈Java中的System.gc()的工作原理
- 9-4-Linear Regression for Binary Classification
- Tiny210 nfs挂载文件
- XRay1
- 有关java语言的话题,也许有你不知道的事