Description

The new founded Balkan Investment Group Bank (BIG-Bank) opened a new office in Bucharest, equipped with a modern computing environment provided by IBM Romania, and using modern information technologies. As usual, each client of the bank is identified by a positive integer K and, upon arriving to the bank for some services, he or she receives a positive integer priority P. One of the inventions of the young managers of the bank shocked the software engineer of the serving system. They proposed to break the tradition by sometimes calling the serving desk with the lowest priority instead of that with the highest priority. Thus, the system will receive the following types of request:

0 The system needs to stop serving
K P Add client K to the waiting list with priority P
2 Serve the client with the highest priority and drop him or her from the waiting list
3 Serve the client with the lowest priority and drop him or her from the waiting list

Your task is to help the software engineer of the bank by writing a program to implement the requested serving policy.

Input

Each line of the input contains one of the possible requests; only the last line contains the stop-request (code 0). You may assume that when there is a request to include a new client in the list (code 1), there is no other request in the list of the same client or with the same priority. An identifier K is always less than 106, and a priority P is less than 107. The client may arrive for being served multiple times, and each time may obtain a different priority.

Output

For each request with code 2 or 3, the program has to print, in a separate line of the standard output, the identifier of the served client. If the request arrives when the waiting list is empty, then the program prints zero (0) to the output.

Sample Input

  1. 2
  2. 1 20 14
  3. 1 30 3
  4. 2
  5. 1 10 99
  6. 3
  7. 2
  8. 2
  9. 0

Sample Output

  1. 0
  2. 20
  3. 30
  4. 10
  5. 0
  6.  
  7. 每个顾客有个编号和优先级,银行每次可以添加顾客的要求进队列,且保证队列中当前任意顾客的编号和优先级都不同.银行可以执行先服务最大优先级的顾客或者先服务最小优先级的顾客操作.对于每个服务,输出顾客的编号.
    按照优先级插入treap,左儿子优先级大于父亲,右儿子小于
  1. #include<cstdio>
  2. #include<cstdlib>
  3. #include<cstring>
  4. #include<cmath>
  5. #include <algorithm>
  6. using namespace std;
  7. const int maxn=1e5+;
  8. struct Node
  9. {
  10. Node *ch[];
  11. int r;
  12. int v;
  13. int id;
  14. Node(int v,int id):v(v),id(id){ch[]=ch[]=NULL;r=((rand()<<)|rand()); }
  15. inline int cmp(int x) const {
  16. if (x==v) return -;
  17. else return x>v?:;
  18. }
  19. };
  20. void rotate(Node* &o,int d) {
  21. Node* k=o->ch[d^];
  22. o->ch[d^]=k->ch[d];
  23. k->ch[d]=o;
  24. o=k;
  25. }
  26. void insert(Node* &o,int x,int id) {
  27. if(o==NULL) {
  28. o=new Node(x,id);
  29. }
  30. else {
  31. int d=x<o->v?:;
  32. insert(o->ch[d],x,id);
  33. if(o->ch[d]->r > o->r)
  34. rotate(o,d^);
  35. }
  36. }
  37. void remove(Node* &o,int x) {
  38. int d=o->cmp(x);
  39. if(d==-) {
  40. Node* u=o;
  41. if(o->ch[]!=NULL&&o->ch[]!=NULL) {
  42. int d2=(o->ch[]->r>o->ch[]->r?:);
  43. rotate(o,d2);
  44. remove(o->ch[d2],x);
  45. }
  46. else {
  47. if(o->ch[]==NULL) o=o->ch[];
  48. else o=o->ch[];
  49. delete u;
  50. }
  51. }
  52. else {
  53. remove(o->ch[d],x);
  54. }
  55. }
  56. int find_max(Node *o)
  57. {
  58. if (o->ch[]==NULL){
  59. printf("%d\n",o->id);
  60. return o->v;
  61. }
  62. return find_max(o->ch[]);
  63. }
  64. int find_min(Node *o)
  65. {
  66. if (o->ch[]==NULL){
  67. printf("%d\n",o->id);
  68. return o->v;
  69. }
  70. return find_min(o->ch[]);
  71. }
  72.  
  73. int main()
  74. {
  75. //freopen("de.txt","r",stdin);
  76. Node *root=NULL;
  77. int op;
  78. while (~scanf("%d",&op)){
  79. if (op==) break;
  80. if (op==){
  81. int id,v;
  82. scanf("%d%d",&id,&v);
  83. //printf("ins %d %d \n",id,v);
  84. insert(root,v,id);
  85. }
  86. else if (op==){
  87. if (root==NULL) {
  88. printf("0\n");
  89. continue;
  90. }
  91. int v = find_max(root);
  92. remove(root,v);
  93. }
  94. else if (op==){
  95. if (root==NULL) {
  96. printf("0\n");
  97. continue;
  98. }
  99. int v = find_min(root);
  100. remove(root,v);
  101. }
  102. }
  103. return ;
  104. }
  1.  

POJ 3481 Double Queue (treap模板)的更多相关文章

  1. POJ 3481 Double Queue STLmap和set新学到的一点用法

    2013-08-08 POJ 3481  Double Queue 这个题应该是STL里较简单的吧,用平衡二叉树也可以做,但是自己掌握不够- -,开始想用两个优先队列,一个从大到小,一个从小到大,可是 ...

  2. POJ 3481 Double Queue(Treap模板题)

    Double Queue Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 15786   Accepted: 6998 Des ...

  3. POJ 3481 Double Queue(STL)

    题意  模拟银行的排队系统  有三种操作  1-加入优先级为p 编号为k的人到队列  2-服务当前优先级最大的   3-服务当前优先级最小的  0-退出系统 能够用stl中的map   由于map本身 ...

  4. POJ 3481 Double Queue(set实现)

    Double Queue The new founded Balkan Investment Group Bank (BIG-Bank) opened a new office in Buchares ...

  5. POJ 3481 Double Queue

    平衡树.. 熟悉些fhq-Treap,为啥我在poj读入优化不能用啊 #include <iostream> #include <cstdio> #include <ct ...

  6. poj 3841 Double Queue (AVL树入门)

    /****************************************************************** 题目: Double Queue(poj 3481) 链接: h ...

  7. POJ-3481 Double Queue,Treap树和set花式水过!

                                                    Double Queue 本打算学二叉树,单纯的二叉树感觉也就那几种遍历了, 无意中看到了这个题,然后就 ...

  8. BZOJ 1588: Treap 模板

    1588: [HNOI2002]营业额统计 Time Limit: 5 Sec  Memory Limit: 162 MBSubmit: 12171  Solved: 4352 Description ...

  9. hdu 1908 Double Queue

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1908 Double Queue Description The new founded Balkan ...

随机推荐

  1. 设计模式 - 装饰器模式(Decorator)

    简介 场景 通过继承和关联都可以给对象增加行为,区别如下: 继承是静态的(无法在程序运行时动态扩展),且作用于所有子类.硬编码,高耦合. 通过装饰器可以在运行时添加行为和属性到指定对象.关联关系就是在 ...

  2. ADFS 2016 & Dynamics CRM

    参考:https://blog.csdn.net/vic0228/article/details/80188291 webapp 获取token https://adfs.demo.local/adf ...

  3. (appium+python)UI自动化_09_unittest批量运行测试用例&生成测试报告

    前言 上篇文章[(appium+python)UI自动化_08_unittest编写测试用例]讲到如何使用unittets编写测试用例,并执行测试文件.接下来讲解下unittest如何批量执行测试文件 ...

  4. Hadoop(2): Blocks存储管理及读写

    1. Replication: 因为每个HDFS被部署在是低成本的商业硬件上(low cost commodity hardware),所以为了有更佳的Fault Tolerance,HDFS将每个B ...

  5. JavaScript高级程序设计(第3版) 第三章 (基本概念)

    3.1 语法 1.不以数字开头的数字,字母,下划线,美元符号 2.注释:html <!-- --> css/**/ js单行// 多行/**/ 3.ES5 引入了严格模式(strict m ...

  6. git报错-Initial commit Untracked files nothing added to commit but untracked ……

    文章转自 https://www.jianshu.com/p/61c3db30d488 在目标执行命令 git stratus 报错 根据上面的文章,可以解决问题.不行的话,请留言. 感谢你的阅读

  7. Lambda -语法使用,代码简化

    使用Lambda的方式实现线程 线程中()是run方法的(),可用来接受参数,格式: new Thread(()->{ System.out.println(Thread.currentThre ...

  8. Dockerfile设置时区alpine

    背景: 最近在写golang相关代码.其中用到了时间操作的相关函数,如下: nowTime := time.Now() nUnixEndTime := nowTime.Unix() nHour, nM ...

  9. python开发之路-day02

    一.数据类型 1 什么是数据? name='sunkedong'#字符串类型 age=24 #整型 date=2017.9#浮点型 dic={'name':'sunkedong','age':16}# ...

  10. Python : Polymorphism

    class Animal: def __init__(self, name): # Constructor of the class self.name = name def talk(self): ...