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 ...
随机推荐
- C++ 构造函数讲解
构造函数是C++.Java等面向对象编程语言不可绕过的话题,构造函数的作用就是初始化所创建对象的数据成员.不过与Java相比C++的构造函数显得更为复杂.因此,现在便来讲解一下C++的构造函数,以此进 ...
- centos下redis和nginx软件的安装
我们这章以redis和nginx软件为例,介绍一下centos下软件的安装. 需要软件包(可以先下载好复制到centos指定目录下,也可直接用wget命令获取): nginx-1.7.9.tar re ...
- SharedPreferences的基本用法
获取SharedPreferences的两种方式: 1 调用Context对象的getSharedPreferences()方法 2 调用Activity对象的getPreferences()方法 两 ...
- 什么是AJAX技术及其常识
1.什么是Ajax? Ajax的全称是:AsynchronousJavaScript+XML 2.Ajax的定义: Ajax不是一个技术,它实际上是几种技术,每种技术都有其独特这处,合在一起就成了一个 ...
- 启动 mysql 失败 Warning:The /usr/local/mysql/data directory is not owned by the 'mysql' or '_mysql'
Warning:The /usr/local/mysql/data directory is not owned by the 'mysql' or '_mysql' 这应该是某种情况下导致/usr/ ...
- 单选复选按钮以及Toast学习笔记
1:单选按钮是以组的形式呈现,xml布局文件中需要定义一个RadioGroup,然后在这个组内再定义RadioButton.在java代码中为该按钮添加监听时,需要用组名来引用对应的方法setOnCh ...
- Android Error:You must supply a layout_width attribute……
出现这种情况的可能原因目前本人碰到的有: 1:在xml文件中某个属性名或者属性值写错,请务必仔细检查你有没有写错某个拼写. 2:当你在,比如TextView中,没有声明layout_width,经测试 ...
- iblog语法高亮示例
-------------------------------------------------------------------------------------- iblog 是一款 Sub ...
- Python支持中文注释
三处设置,使Python的Eclipse开发环境(使用PyDev)支持中文 - (a)Eclipse的Window菜单Editors设置: Eclipse工具条 -> Window -> ...
- DC-EPC小结
今晚上修完了最后2个学时的EPC(课程主页),这意味着本学期的DC和共20个学时的EPC到此结束,这有可能是我人生中最后一次上英语课. Tom是我DC课的老师,EPC起于Tom和Micheal的Deb ...