zoukankan      html  css  js  c++  java
  • POJ 3481 Double Queue (treap模板)

    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

    2
    1 20 14
    1 30 3
    2
    1 10 99
    3
    2
    2
    0

    Sample Output

    0
    20
    30
    10
    0

    每个顾客有个编号和优先级,银行每次可以添加顾客的要求进队列,且保证队列中当前任意顾客的编号和优先级都不同.银行可以执行先服务最大优先级的顾客或者先服务最小优先级的顾客操作.对于每个服务,输出顾客的编号.
    按照优先级插入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+10;
      8 struct Node
      9 {
     10     Node *ch[2];
     11     int r;
     12     int v;
     13     int id;
     14     Node(int v,int id):v(v),id(id){ch[0]=ch[1]=NULL;r=((rand()<<15)|rand()); }
     15     inline int cmp(int x) const {
     16         if (x==v) return -1;
     17         else return x>v?0:1;
     18     }
     19 };
     20 void  rotate(Node* &o,int d) {
     21     Node* k=o->ch[d^1];
     22     o->ch[d^1]=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?1:0;
     32         insert(o->ch[d],x,id);
     33         if(o->ch[d]->r > o->r)
     34         rotate(o,d^1);
     35     }
     36 }
     37 void remove(Node* &o,int x) {
     38     int d=o->cmp(x);
     39     if(d==-1) {
     40         Node* u=o;
     41         if(o->ch[0]!=NULL&&o->ch[1]!=NULL) {
     42             int d2=(o->ch[0]->r>o->ch[1]->r?1:0);
     43             rotate(o,d2);
     44             remove(o->ch[d2],x);
     45         }
     46         else {
     47             if(o->ch[0]==NULL) o=o->ch[1];
     48             else o=o->ch[0];
     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[0]==NULL){
     59         printf("%d
    ",o->id);
     60         return o->v;
     61     }
     62     return find_max(o->ch[0]);
     63 }
     64 int find_min(Node *o)
     65 {
     66     if (o->ch[1]==NULL){
     67         printf("%d
    ",o->id);
     68         return o->v;
     69     }
     70     return find_min(o->ch[1]);
     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==0) break;
     80         if (op==1){
     81             int id,v;
     82             scanf("%d%d",&id,&v);
     83             //printf("ins %d %d 
    ",id,v);
     84             insert(root,v,id);
     85         }
     86         else if (op==2){
     87             if (root==NULL) {
     88                 printf("0
    ");
     89                 continue;
     90             }
     91             int v = find_max(root);
     92             remove(root,v);
     93         }
     94         else if (op==3){
     95             if (root==NULL) {
     96                 printf("0
    ");
     97                 continue;
     98             }
     99             int v = find_min(root);
    100             remove(root,v);
    101         }
    102     }
    103     return 0;
    104 }
     
  • 相关阅读:
    router基本使用
    函数声明 和 var声明的优先级
    适用于Windows桌面应用程序的.NET Core 3
    在.Net Core 3.0中尝试新的System.Text.Json API
    在WPF中使用.NET Core 3.0依赖项注入和服务提供程序
    WPF控件获得焦点时去除虚线框
    Call asynchronous method in constructor
    将自定义控件加载到RichTextbox并进行交互
    WPF应用无法使用Snoop分析的解决办法
    关于序列化和反序列化
  • 原文地址:https://www.cnblogs.com/agenthtb/p/7672764.html
Copyright © 2011-2022 走看看