1. #include <iostream>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <algorithm>
  5.  
  6. using namespace std;
  7. const int maxn=+;
  8. int heap_size=; //堆的元素个数
  9. struct Node{
  10. char name[];
  11. int para,pri;
  12. int t; //用于存储信息加入的顺序,当优先级相同时,t小的先出队列
  13. }node[maxn];
  14.  
  15. //交换node[a]和node[b]的值
  16. void exchange(int a,int b){
  17. Node tmp;
  18. tmp=node[a];
  19. node[a]=node[b];
  20. node[b]=tmp;
  21. }
  22. //比较node[a]和node[b],若a小于,则返回-1。
  23. int compare(int a,int b){
  24. if(node[a].pri<node[b].pri)
  25. return -;
  26. if(node[a].pri>node[b].pri)
  27. return ;
  28. if(node[a].t<node[b].t)
  29. return -;
  30. if(node[a].t>node[b].t)
  31. return ;
  32. return ;
  33. }
  34. //维护堆,使堆保持最小堆的性质
  35. void heap_update(int i){
  36. int small;
  37. int l=*i;
  38. int r=*i+;
  39. if(l<=heap_size && compare(l,i)<)
  40. small=l;
  41. else
  42. small=i;
  43. if(r<=heap_size && compare(r,small)<)
  44. small=r;
  45. if(small!=i){
  46. exchange(i,small);
  47. heap_update(small);
  48. }
  49. }
  50. //加入一个新元素
  51. void heap_insert(char str[],int para,int pri){
  52. heap_size++;
  53. strcpy(node[heap_size].name,str);
  54. node[heap_size].para=para;
  55. node[heap_size].pri=pri;
  56. node[heap_size].t=heap_size;
  57. int t=heap_size;
  58. while(t> && node[t/].pri>node[t].pri){
  59. exchange(t/,t);
  60. t=t>>;
  61. }
  62. }
  63. //将堆顶元素出队列
  64. Node heap_pop(){
  65. Node tmp=node[];
  66. node[]=node[heap_size];
  67. heap_size--;
  68. heap_update();
  69. return tmp;
  70. }
  71. int main()
  72. {
  73. char str[],s[];
  74. Node tmp;
  75. int para,pri;
  76. while(scanf("%s",str)!=EOF){
  77. if(str[]=='G'){
  78. if(heap_size==){
  79. printf("EMPTY QUEUE!\n");
  80. }
  81. else{
  82. tmp=heap_pop();
  83. printf("%s %d\n",tmp.name,tmp.para);
  84. }
  85. }
  86. else{
  87. scanf("%s%d%d",s,&para,&pri);
  88. heap_insert(s,para,pri);
  89. }
  90. }
  91. return ;
  92. }

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. 大仙说道之Android studio实现Service AIDL

    今天要开发过程中要用到AIDL的调用,之前用的eclipse有大量教程,用起来很方便,现在刚换了Android studio,不可否认studio真的很强大,只是很多功能还需要摸索. AIDL(And ...

  2. Abstract_Factory

    #include <iostream> using namespace std; #define DESTORY_POINTER(ptr) if (ptr) { delete ptr; p ...

  3. 【Qt】Qt实战一二三【转】

    简介 “我们来自Qt分享&&交流,我们来自Qt Quick分享&&交流”,不管你是笑了,还是笑了,反正我们是认真的.我们就是要找寻一种Hold不住的状态,来开始每一天的 ...

  4. 简单实用的PHP验证码类

    一个简单实用的php验证码类,分享出来 ,供大家参考. 代码如下: <?php /** @ php 验证码类 @ http://www.jbxue.com */ Class code { var ...

  5. openerp模块收藏 auto_setup 自动帮你完成建新库时必做几个操作(转载)

    auto_setup 自动帮你完成建新库时必做几个操作 原文:http://shine-it.net/index.php/topic,6777.0.html 模块地址: https://github. ...

  6. uWSGI uwsgi_response_write_body_do(): Connection reset by peer 报错的解决方法

    服务器架构是:Nginx+uWSGI+Django 某一天,发现服务器返回的response不完整,例如文档大小是200K的,但是只返回了100K给浏览器. 查了一下uWSGI的日志,发现以下错误: ...

  7. 世界级Oracle专家Jonathan Lewis:我很为DBA们的未来担心(图灵访谈)

    部分节选 图灵社区:如果您的子女对计算机科学感兴趣,你会让他们选什么具体的方向呢? JL:我的孩子对计算机科学一点都不感兴趣,甚至对科学都没什么兴趣.他们主修的都是艺术.如果要我给其他的人建议的话,我 ...

  8. Gartner2014年魔力象限(商业智能和分析平台)

  9. MYSQL创建多张表,相同表结构,不同表名

    #!/bin/bashfor i in {0..63}domysql -u$1 -p$2 -h127.0.0.1 <<EOFuse yoon;create table ivc_pre_de ...

  10. 栈帧%ebp,%esp详解

    首先应该明白,栈是从高地址向低地址延伸的.每个函数的每次调用,都有它自己独立的一个栈帧,这个栈帧中维持着所需要的各种信息.寄存器ebp指向当前的栈帧的底部(高地址),寄存器esp指向当前的栈帧的顶部( ...