poj 3481 Double Queue(treap)
2015-11-15 10:21
357 查看
Double Queue
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:
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
Sample Output
题意:1表示插入客户K,他的优先级是P(相当于大小),2表示输出当前优先级最高的客户(即找出最大值),并且删除。3同理输出最低级的。
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 |
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 |
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
题意: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; }
相关文章推荐
- Java中改变应用程序界面外观(javax.swing.UIManager类和LookAndFeel类)
- SoupUI简易实用手册
- Why is there extra padding at the top of my UITableView with style UITableViewStyleGrouped in iOS7
- UITableView如何让cell的分割线左边不缩进
- UIView动画和定时器
- Educational Codeforces Round 1 B. Queries on a String 暴力
- 苹果开发 笔记(93) UITableView 的编辑模式
- 如何使用UIAutomation进行iOS 自动化测试(Part I)
- 有一个无效 SelectedValue,因为它不在项目列表中。
- 一. 使用代码实现UITableView的cell自定义高度-类似于微博主页
- IOS的UI基础02
- 为 Key-Value 数据库实现MVCC 事务
- UITableView 滚动到指定行cell
- GPUImage API文档之GPUImageContext类
- UISlider自定义滑块大小
- ios UiView转换成UIImage
- iOS 开发之带输入框的UIAlertView
- 基于POM(page oriented model)的UI测试脚本编写
- IOS之UIImageView--小实例项目--带音效的拳皇动画
- Arduino与Android蓝牙通信笔记