zoj 2714 Windows Message Queue(优先队列、简单的二叉堆处理)
2014-08-03 11:43
316 查看
Windows Message Queue
点击打开题目链接
Time Limit: 2 Seconds
Memory Limit: 65536 KB
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
2;删除(下降操作)
先用树尾元素将堆根元素覆盖,此时失去了二叉堆的堆序性,比较其子节点较小者,比较key值交换;
以此不断比较交换,知道恢复堆序性;
[/code]
点击打开题目链接
Time Limit: 2 Seconds
Memory Limit: 65536 KB
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! 根据优先级输出:明显的优先队列(需自定义优先级) 1、优先队列的实现方式(stl库函数) 头文件:#include<queue> 优先队列相比普通的队列在于,优先队列不一定是先进先出,出的时候是按相应的优先级,优先级高的先出; 关于优先队列具体用法的请参考博客/article/2469942.html 此题实现代码:#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值,如果比父节点小(或者比父节点大(大根堆))则交换,以此不断比较交换来恢复二叉堆的堆序性;
[code]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 60010 using 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; }
[/code]
相关文章推荐
- zoj 2724 Windows Message Queue 优先队列
- HDOJ 1509 Windows Message Queue (简单优先队列 )
- zoj 2724 Windows Message Queue 优先队列
- hud 1509 Windows Message Queue 简单优先队列
- ZOJ 2724 Windows Message Queue (二叉堆,优先队列)
- hdoj 1509 Windows Message Queue 【STL 优先队列】
- 优先队列的简单实现-二叉堆实现
- HDU - 1509 Windows Message Queue (优先队列 重载运算符)
- HDU - 1509 Windows Message Queue (优先队列 重载运算符)
- (二叉堆10.2.1)ZOJ 2724 Windows Message Queue(对结构体使用优先队列)
- HDU 1509:Windows Message Queue【优先队列】
- HDUoj 1509 Windows Message Queue ( 优先队列
- hdu1509 Windows Message Queue(优先队列水)
- hrbust 哈理工oj 1246 Windows Message Queue【优先队列】
- HDU - 1509 Windows Message Queue (优先队列 重载运算符)
- Hdoj 1509 -Windows Message Queue 优先队列最小堆实现
- ZOJ 2724 Windows Message Queue (二叉堆,优先队列)
- hdoj 1509 Windows Message Queue【优先队列】
- Hdoj 1509 -Windows Message Queue 优先队列最小堆实现
- HDU - 1509 Windows Message Queue (优先队列 重载运算符)