zoj 2714 Windows Message Queue(优先队列、简单的二叉堆处理)
来源:互联网 发布:淘宝灯具安装联盟 编辑:程序博客网 时间:2024/06/06 05:42
点击打开题目链接
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!根据优先级输出:明显的优先队列(需自定义优先级)1、优先队列的实现方式(stl库函数)头文件:#include<queue>优先队列相比普通的队列在于,优先队列不一定是先进先出,出的时候是按相应的优先级,优先级高的先出; 关于优先队列具体用法的请参考博客http://blog.csdn.net/dooder_daodao/article/details/5761550此题实现代码:#include <iostream>#include<stdio.h>#include<string.h>#include<queue>using namespace std;struct node{ char name[100]; int para; int pro; friend bool operator<(const node &n1,const node &n2)//自定义优先级 { return n1.pro>n2.pro; }};int main(){ char op[10]; priority_queue<node> heap; while(~scanf("%s",op)) { node p; if(strcmp(op,"PUT")==0) { scanf("%s%d%d",p.name,&p.para,&p.pro); heap.push(p); } else { if(!heap.empty()) { node top=heap.top(); printf("%s %d\n",top.name,top.para); heap.pop(); } else printf("EMPTY QUEUE!\n"); } memset(op,'\0',sizeof(op)); } return 0;}
2.二叉堆实现;
二叉堆:
二叉堆是一种特殊的堆,二叉堆是完全二元树或者是近似完全二元树。二叉堆有两种:最大堆和最小堆。最大堆:父结点的键值总是大于或等于任何一个子节点的键值;
最小堆:父结点的键值总是小于或等于任何一个子节点的键值。
二叉堆的几个基本操作:
1.插入(上升操作):
先将插入的元素放到树尾,然后比较和其父节点的key值,如果比父节点小(或者比父节点大(大根堆))则交换,以此不断比较交换来恢复二叉堆的堆序性;void add(){ int k=++total; heap[k]=used++;//挂在树尾 while(k>1)//上升操作 { int t=k/2; if(cmp(heap[t],heap[k])>0) { swap(heap[t],heap[k]); k=t; } else break; }}
2;删除(下降操作)
先用树尾元素将堆根元素覆盖,此时失去了二叉堆的堆序性,比较其子节点较小者,比较key值交换;
以此不断比较交换,知道恢复堆序性;void delete_heap()/?删除操作(并没有真正的删除){ int k=1; heap[k]=heap[total--];//覆盖 while(k*2<=total)/比较恢复堆序性 { int t=k*2; if(t<total&&cmp(heap[t+1],heap[t])<0) ++t; if(cmp(heap[t],heap[k])<0) { swap(heap[t],heap[k]); k=t; } else break; }}此题代码:<pre name="code" class="cpp">#include <iostream>#include <stdio.h>#include <string.h>#define MAX 60010using namespace std;struct node{ char name[100]; int para; int pro,t;}p[MAX];int heap[MAX];int top,used;int total;int cmp(int a,int b){ if(p[a].pro<p[b].pro) return -1; if(p[a].pro>p[b].pro) return 1; if(p[a].t<p[b].t) return -1; if(p[a].t>p[b].t) return 1; return 0;}void swap(int &a,int &b){ int t; t=b; b=a; a=t;}void add(){ int k=++total; heap[k]=used++; while(k>1) { int t=k/2; if(cmp(heap[t],heap[k])>0) { swap(heap[t],heap[k]); k=t; } else break; }}void delete_heap(){ int k=1; heap[k]=heap[total--]; while(k*2<=total) { int t=k*2; if(t<total&&cmp(heap[t+1],heap[t])<0) ++t; if(cmp(heap[t],heap[k])<0) { swap(heap[t],heap[k]); k=t; } else break; }}int main(){ char op[10]; int cnt=0; used=0; total=0; // freopen("\\input.txt","r",stdin); // freopen("\\output.txt","w",stdout); while(~scanf("%s",op)) { if(strcmp(op,"PUT")==0) { getchar(); scanf("%s%d%d",p[used].name,&p[used].para,&p[used].pro); p[used].t=cnt++; add(); } else { if(total!=0) { printf("%s %d\n",p[heap[1]].name,p[heap[1]].para); delete_heap(); } else printf("EMPTY QUEUE!\n"); } memset(op,'\0',sizeof(op)); } return 0;}
- zoj 2714 Windows Message Queue(优先队列、简单的二叉堆处理)
- zoj-2724 Windows Message Queue(二叉堆&&优先队列)
- ZOJ 2724 Windows Message Queue (二叉堆,优先队列)
- ZOJ 2724 Windows Message Queue(二叉堆||优先队列)
- (二叉堆10.2.1)ZOJ 2724 Windows Message Queue(对结构体使用优先队列)
- ZOJ 2724 Windows Message Queue(二叉堆or优先队列)
- zoj 2724 Windows Message Queue 优先队列
- ZOJ 2724/HDU 1509 Windows Message Queue(优先队列)
- Hdoj 1509 -Windows Message Queue 优先队列最小堆实现
- hud 1509 Windows Message Queue 简单优先队列
- HDOJ 1509 Windows Message Queue (简单优先队列 )
- hdu 1509 Windows Message Queue (简单STL的优先队列应用)
- Windows Message Queue (优先队列的应用)
- ZOJ 2724 Windows Message Queue 堆
- hdu1509 Windows Message Queue(优先队列)
- Windows Message Queue(priority_queue优先队列)
- hdu 1509 Windows Message Queue 优先队列
- hdoj1509 Windows Message Queue (优先队列)
- 清和月
- RCP项目导出exe 步骤
- asp.net MVC 安全性
- 结构体指针内存——指针数组——字符串指针内存申请
- vs2010 命令行参数的简单写法
- zoj 2714 Windows Message Queue(优先队列、简单的二叉堆处理)
- poj2418Hardwood Species
- C程序编译执行过程
- 全局变量与局部变量的区别
- ARM Linux&设备树(Device Tree)
- 支持Box2D,开源HTML5 2D游戏引擎FLAG
- cocos2dx中的动作使用
- JS函数集
- UI----label的基本属性 imageView的基本属性