#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,&para,&pri);
heap_insert(s,para,pri);
}
}
return ;
}

ZOJ 2724 Windows Message Queue (优先级队列,水题,自己动手写了个最小堆)的更多相关文章

  1. zoj 2724 Windows Message Queue

    Windows Message Queue Time Limit: 2 Seconds      Memory Limit: 65536 KB Message queue is the basic f ...

  2. zoj 2724 Windows Message Queue(使用priority_queue容器模拟消息队列)

    题目链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2724 题目描述: Message queue is the b ...

  3. ACM解题之(ZOJ 2724)Windows Message Queue

    题目来源: 点击打开链接 题目翻译: 消息队列是windows系统的基本基础.对于每个进程,系统都维护一个消息队列.如果这个过程发生某些事情,例如鼠标点击,文本改变,系统会向队列添加一条消息.同时,如 ...

  4. ZOJ 2724 Windows Message Queue (二叉堆,优先队列)

    思路:用优先队列 priority_queue,简单 两种方式改变队列 的优先级 (默认的是从大到小) #include<iostream> #include<queue> # ...

  5. zoj 2724 Windows Message Queue 优先队列

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1724 题目大意: 给出两种操作,GET要求取出当前队首的元素,而PUT会输入名 ...

  6. uva 12100 Printer Queue 优先级队列模拟题 数组模拟队列

    题目很简单,给一个队列以及文件的位置,然后一个一个检查,如果第一个是优先级最高的就打印,否则放到队列后面,求所要打印的文件打印需要花费多长时间. 这里我用数组模拟队列实现,考虑到最糟糕的情况,必须把数 ...

  7. Windows Message Queue(优先队列)

    欢迎参加——BestCoder周年纪念赛(高质量题目+多重奖励) Windows Message Queue Time Limit: 2000/1000 MS (Java/Others)    Mem ...

  8. Windows Message Queue

    Windows Message Queue Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...

  9. hdu 1509 Windows Message Queue

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1509 Windows Message Queue Description Message queue ...

随机推荐

  1. TCP通信三次握手的过程

    过程 编辑 第一次 第一次握手:建立连接时,客户端 发送syn包(syn=j)到服务器,并进入SYN_SENT状态,等待服务器确认:SYN:同步序列编号(Synchronize Sequence Nu ...

  2. java.util.TreeSet源码分析

    TreeSet是基于TreeMap实现的,元素的顺序取决于元素自身的自然顺序或者在构造时提供的比较器. 对于add,remove,contains操作,保证log(n)的时间复杂度. 因为Set接口的 ...

  3. 【风马一族_Android】Android Studio 给APP设置签名

    在Android Studio中,给App签名,如果没有给App设置签名的话,Android Studio会主动给app设置一个默认的签名 接下来,介绍主动给App设置一个签名的整个步骤过程: 1) ...

  4. 《DNS加密更新》RHEL6

    DNS加密更新: 继DNS更新之后,现在又玩DNS加密更新,差不多. DNS更新指定一台主机,那台主机或多台主机来更新它,其他主机没权限. DNS加密更新,谁有密码说就可以更新. 做过上次的更新之后, ...

  5. javascript对象与实例

    var person=new Object(); var person2={}; 一切都是对象 person是Object的实例, 也是对象. 第二个person2也是如此,只不过它是采用字面量的方式 ...

  6. js设计模式(3)---桥接模式

    0.前言 看设计模式比较痛苦,一则是自己经验尚浅,不能体会到使用这些设计模式的益处:二则是不能很好把握使用这些设计模式的时机.所以这一部分看得断断续续,拖拖拉拉,为了了却这快心病,决定最近一口气看完几 ...

  7. UVA10361 -自动作诗机

    UVA10361 - Automatic Poetry(自动作诗机) A Schuttelreim seems to be a typical German invention. The funny ...

  8. RHEL安装docker-compose

    Note that Compose 1.5.2 requires Docker 1.7.1 or later. pip install docker-compose==1.5.2 Note that ...

  9. Crusher Django 学习笔记1 hello world

    http://crusher-milling.blogspot.com/2013/09/crusher-django-tutorial1-hello-world.html 随便学习一下goagent ...

  10. Optimize date2str function

    /*************************************************** Created Date: 19 Jul 2013 Created By: Jimmy Xie ...