博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ3481 Double Queue
阅读量:6485 次
发布时间:2019-06-23

本文共 3699 字,大约阅读时间需要 12 分钟。

Double Queue
Time Limit: 1000MS   Memory Limit: 65536K
     

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
1 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

21 20 141 30 321 10 993220

Sample Output

02030100

Source

 
最简单的平衡树题,写的是treap。
codes:
1 #include
2 #include
3 #include
4 #include
5 #include
6 #include
7 #include
8 #include
9 using namespace std;10 const int N = 100100;11 #define For(i,n) for(int i=1;i<=n;i++)12 #define Rep(i,l,r) for(int i=l;i<=r;i++)13 14 struct treap{15 int s[2],pri,v,key;16 void init(int x,int y,int z){17 v = x; key = y; pri = z;18 }19 }T[N];20 21 int op,rt,root,tot,v,key;22 23 void Rot(int &y,int f){24 int x = T[y].s[!f];25 T[y].s[!f] = T[x].s[f];26 T[x].s[f] = y;27 y = x;28 }29 30 void Insert(int &i,int v,int key){31 if(!i){32 T[i=++tot].init(v,key,rand());33 return;34 } 35 int f = T[i].key > key;36 Insert(T[i].s[!f],v,key);37 if(T[T[i].s[!f]].pri > T[i].pri) Rot(i,f);38 }39 40 void Delete(int &i,int key){41 if(T[i].key == key){42 if(!T[i].s[0]||!T[i].s[1]){43 if(!T[i].s[0]) i = T[i].s[1];44 else i = T[i].s[0];45 }46 else{47 int f = T[T[i].s[0]].pri > T[T[i].s[1]].pri;48 Rot(i,f);49 Delete(T[i].s[f],key);50 }51 }else Delete(T[i].s[key>T[i].key],key);52 }53 54 int read(){55 char ch = getchar(); int num = 0 , q = 1;56 while(ch>'9'||ch<'0'){57 if(ch=='-') q = -1;58 ch = getchar();59 }60 while(ch>='0'&&ch<='9'){61 num = num * 10 + ch - '0';62 ch = getchar();63 }64 return num * q;65 }66 67 int Get(int i,int f){68 int rt = i;69 while(T[rt].s[f]) rt = T[rt].s[f];70 return rt;71 }72 73 int main(){74 srand(time(NULL));75 while(op = read(),op){76 if(op==1){77 v = read(); key = read();78 Insert(root,v,key);79 }else{80 if(op==2) rt = Get(root,1);81 else rt = Get(root,0);82 printf("%d\n",T[rt].v);83 Delete(root,T[rt].key);84 }85 }86 return 0;87 }

 

转载于:https://www.cnblogs.com/zjdx1998/p/3883844.html

你可能感兴趣的文章
搭建ubuntu环境
查看>>
Xen命令全集
查看>>
水环境指标 中文对照
查看>>
PROC系列之---/proc/stat
查看>>
YUM
查看>>
Web App和Native App 谁将是未来
查看>>
Git 常用命令整理
查看>>
hive 导入数据表乱码
查看>>
Java 多线程 之 Thread
查看>>
配置管理小报100330:为什么配置库中代码和文档分开放?
查看>>
JSP指令元素:page指令,include指令,taglib指令
查看>>
java 自动装箱和拆箱
查看>>
NTFS的五大热点问题解答
查看>>
下丁字符号用MathType怎么编辑出来
查看>>
Java Web对mysql数据库的几种操作
查看>>
Android Studio插件
查看>>
CoreText进阶(五)- 文字排版样式和效果
查看>>
java中的访问权限
查看>>
CallableAndFuture
查看>>
Nginx配置文件详细说明
查看>>