poj 3481 Double Queue(treap)
来源:互联网 发布:服装电脑制版软件 编辑:程序博客网 时间:2024/06/06 01:34
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:
0The system needs to stop serving1 K PAdd client K to the waiting list with priority P2Serve the client with the highest priority and drop him or her from the waiting list3Serve the client with the lowest priority and drop him or her from the waiting listYour 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
题意:1表示插入客户K,他的优先级是P(相当于大小),2表示输出当前优先级最高的客户(即找出最大值),并且删除。3同理输出最低级的。
treap入门
#include <stdio.h>#include <string.h>#include <iostream>#include <algorithm>using namespace std;const int Max=100005;const int INF=0x3f3f3f3f;int tot;int root;struct TreapTree{ int id; //堆依据 int priority; //bst依据 int child[2]; inline void set(int id,int priority) { this->id=id; this->priority=priority; child[0]=child[1]=0; } }T[Max];//旋转 kind=0 左旋,kind=1右旋 (x为旋转结点的父节点)void Rotate(int &x,int kind){ int y=T[x].child[!kind]; T[x].child[!kind]=T[y].child[kind]; T[y].child[kind]=x; x=y;}void insert(int &x,int id,int priority){ if (x==0) { T[x=++tot].set(id, priority); return; } else { int kind=priority<T[x].priority; insert(T[x].child[!kind], id, priority); if (T[x].id<T[T[x].child[!kind]].id) Rotate(x, kind); }}void remove(int &x,int priority){ if (T[x].priority==priority) { if (T[x].child[0] && T[x].child[1]) { int kind=T[T[x].child[0]].id>T[T[x].child[1]].id; Rotate(x, kind); remove(T[x].child[kind],priority); } else { if (!T[x].child[0]) x=T[x].child[1]; else x=T[x].child[0]; } } else { int kind=T[x].priority>priority; remove(T[x].child[!kind],priority); }}int get_obj(int x,int kind){ while (T[x].child[kind]) x=T[x].child[kind]; return x;}int main(){ int n,id, priority, x; tot=0; root=0; while(scanf("%d", &n) && n) { switch (n) { case 1: scanf("%d%d", &id, &priority); insert(root, id, priority); break; case 2: x=get_obj(root, 1); if(x) { printf("%d\n",T[x].id); remove(root, T[x].priority); } else printf("0\n"); break; case 3: x=get_obj(root, 0); if(x) { printf("%d\n",T[x].id); remove(root, T[x].priority); } else printf("0\n"); } } return 0;}
- POJ 3481 Double Queue(Treap)
- poj 3481 Double Queue(treap)
- POJ 3481 Double Queue Treap
- POJ 3481 Double Queue Treap
- poj 3481 Double Queue (Treap)
- POJ 3481 Double Queue SBT || treap
- poj 3481 Double Queue
- Double Queue POJ 3481
- POJ 3481 Double Queue
- POJ 3481 Double Queue
- POJ 3481 Double Queue
- POJ 3481 Double Queue
- POJ 3481 Double Queue
- poj 3481 Double Queue
- poj 3481 Double Queue
- POJ - 3481 Double Queue
- POJ 3481 Double Queue
- POJ 3481 Double queue
- Java中访问控制符的具体用法
- 监听器(Listener)
- 最短路径树问题
- android notification通知栏方式更新APP
- CodeForces 44B Cola
- poj 3481 Double Queue(treap)
- 关于GCJ02和WGS84坐标系对比
- 回文数字
- 001-编辑器工作区 srt字幕文件
- 数字游戏
- 002-unity 资源及资源类型 srt字幕文件
- typedef struct,struct
- 使用mysql时遇到的各种问题
- Socket心跳包异常检测的C语言实现,服务器与客户端代码案例