ZOJ 2724 Windows Message Queue (优先级队列,水题,自己动手写了个最小堆)
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm> using namespace std;
const int maxn=+;
int heap_size=; //堆的元素个数
struct Node{
char name[];
int para,pri;
int t; //用于存储信息加入的顺序,当优先级相同时,t小的先出队列
}node[maxn]; //交换node[a]和node[b]的值
void exchange(int a,int b){
Node tmp;
tmp=node[a];
node[a]=node[b];
node[b]=tmp;
}
//比较node[a]和node[b],若a小于,则返回-1。
int compare(int a,int b){
if(node[a].pri<node[b].pri)
return -;
if(node[a].pri>node[b].pri)
return ;
if(node[a].t<node[b].t)
return -;
if(node[a].t>node[b].t)
return ;
return ;
}
//维护堆,使堆保持最小堆的性质
void heap_update(int i){
int small;
int l=*i;
int r=*i+;
if(l<=heap_size && compare(l,i)<)
small=l;
else
small=i;
if(r<=heap_size && compare(r,small)<)
small=r;
if(small!=i){
exchange(i,small);
heap_update(small);
}
}
//加入一个新元素
void heap_insert(char str[],int para,int pri){
heap_size++;
strcpy(node[heap_size].name,str);
node[heap_size].para=para;
node[heap_size].pri=pri;
node[heap_size].t=heap_size;
int t=heap_size;
while(t> && node[t/].pri>node[t].pri){
exchange(t/,t);
t=t>>;
}
}
//将堆顶元素出队列
Node heap_pop(){
Node tmp=node[];
node[]=node[heap_size];
heap_size--;
heap_update();
return tmp;
}
int main()
{
char str[],s[];
Node tmp;
int para,pri;
while(scanf("%s",str)!=EOF){
if(str[]=='G'){
if(heap_size==){
printf("EMPTY QUEUE!\n");
}
else{
tmp=heap_pop();
printf("%s %d\n",tmp.name,tmp.para);
}
}
else{
scanf("%s%d%d",s,¶,&pri);
heap_insert(s,para,pri);
}
}
return ;
}
ZOJ 2724 Windows Message Queue (优先级队列,水题,自己动手写了个最小堆)的更多相关文章
- zoj 2724 Windows Message Queue
Windows Message Queue Time Limit: 2 Seconds Memory Limit: 65536 KB Message queue is the basic f ...
- zoj 2724 Windows Message Queue(使用priority_queue容器模拟消息队列)
题目链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2724 题目描述: Message queue is the b ...
- ACM解题之(ZOJ 2724)Windows Message Queue
题目来源: 点击打开链接 题目翻译: 消息队列是windows系统的基本基础.对于每个进程,系统都维护一个消息队列.如果这个过程发生某些事情,例如鼠标点击,文本改变,系统会向队列添加一条消息.同时,如 ...
- ZOJ 2724 Windows Message Queue (二叉堆,优先队列)
思路:用优先队列 priority_queue,简单 两种方式改变队列 的优先级 (默认的是从大到小) #include<iostream> #include<queue> # ...
- zoj 2724 Windows Message Queue 优先队列
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1724 题目大意: 给出两种操作,GET要求取出当前队首的元素,而PUT会输入名 ...
- uva 12100 Printer Queue 优先级队列模拟题 数组模拟队列
题目很简单,给一个队列以及文件的位置,然后一个一个检查,如果第一个是优先级最高的就打印,否则放到队列后面,求所要打印的文件打印需要花费多长时间. 这里我用数组模拟队列实现,考虑到最糟糕的情况,必须把数 ...
- Windows Message Queue(优先队列)
欢迎参加——BestCoder周年纪念赛(高质量题目+多重奖励) Windows Message Queue Time Limit: 2000/1000 MS (Java/Others) Mem ...
- Windows Message Queue
Windows Message Queue Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- hdu 1509 Windows Message Queue
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1509 Windows Message Queue Description Message queue ...
随机推荐
- Azure + vsftpd + ubntu14 + 虚拟用户 遇到的问题:从网上摘抄
:在Azure安装好ubuntu虚拟机,安装包选择ubuntu14.04 LTS,选择LTS,以便微软对其以后的更好支持,不选就可能技术支持不会很久,现只开一台,端口默认选择22 :打开虚拟机,进入端 ...
- boost:program_options
由于系统库getopt和getopt_long用起来不够直观,仔细看了下boost发现Boost.Program_options可以满足我的需求,它和getopt系列函数一样,可以抓起命令行参数,这里 ...
- linux 编译内核[scripts/kconfig/dochecklxdialog] 错误
administrator@ubuntu:~/linux-2.6.28-omap$ make menuconfig *** Unable to find the ncurses libraries o ...
- Win7在CMD命令行中使用管理员权限运行命令
使用命令: runas /user:administrator 需要执行的命令 如下:
- [C/C++] 各种C/C++编译器对UTF-8源码文件的兼容性测试(VC、GCC、BCB)
在不同平台上开发C/C++程序时,为了避免源码文件乱码,得采用UTF-8编码来存储源码文件.但是很多编译器对UTF-8源码文件兼容性不佳,于是我做了一些测试,分析了最佳保存方案. 一.测试程序 为了测 ...
- 第27章 项目8:使用XML-RPC进行文件共享
1.问题 创建一个简单的P2P文件共享程序. P2P文件共享程序是在不同计算机上的程序交换文件.P2P交互内,任何节点(peer)都可以是链接到其他节点.在这样一个由节点组成的虚拟网络中,是没有中央节 ...
- 微软更换考试中心后报名攻略以及MCT半价
微软从Prometric更换为Pearson的考试中心,比起以前的预约流程更加便捷. Pearson VUE为微软公司提供MCP考试服务 Pearson VUE为微软公司提供MCP考试服务 http: ...
- 斐波那契(Fibonacci)数列的七种实现方法
废话不多说,直接上代码 #include "stdio.h" #include "queue" #include "math.h" usin ...
- Oracle重建表索引及手工收集统计信息
Oracle重建所有表的索引的sql: SELECT 'alter index ' || INDEX_NAME || ' rebuild online nologging;' FROM USER_IN ...
- libevent使用
(sudo apt-get install libevent-dev) 1:安装libevent 用wget指令直接下载libevent:# wget http://www.monkey.org/~p ...