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 |
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
2
1 20 14
1 30 3
2
1 10 99
3
2
2
0
Sample Output
0
20
30
10
0 每个顾客有个编号和优先级,银行每次可以添加顾客的要求进队列,且保证队列中当前任意顾客的编号和优先级都不同.银行可以执行先服务最大优先级的顾客或者先服务最小优先级的顾客操作.对于每个服务,输出顾客的编号.
按照优先级插入treap,左儿子优先级大于父亲,右儿子小于
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include <algorithm>
using namespace std;
const int maxn=1e5+;
struct Node
{
Node *ch[];
int r;
int v;
int id;
Node(int v,int id):v(v),id(id){ch[]=ch[]=NULL;r=((rand()<<)|rand()); }
inline int cmp(int x) const {
if (x==v) return -;
else return x>v?:;
}
};
void rotate(Node* &o,int d) {
Node* k=o->ch[d^];
o->ch[d^]=k->ch[d];
k->ch[d]=o;
o=k;
}
void insert(Node* &o,int x,int id) {
if(o==NULL) {
o=new Node(x,id);
}
else {
int d=x<o->v?:;
insert(o->ch[d],x,id);
if(o->ch[d]->r > o->r)
rotate(o,d^);
}
}
void remove(Node* &o,int x) {
int d=o->cmp(x);
if(d==-) {
Node* u=o;
if(o->ch[]!=NULL&&o->ch[]!=NULL) {
int d2=(o->ch[]->r>o->ch[]->r?:);
rotate(o,d2);
remove(o->ch[d2],x);
}
else {
if(o->ch[]==NULL) o=o->ch[];
else o=o->ch[];
delete u;
}
}
else {
remove(o->ch[d],x);
}
}
int find_max(Node *o)
{
if (o->ch[]==NULL){
printf("%d\n",o->id);
return o->v;
}
return find_max(o->ch[]);
}
int find_min(Node *o)
{
if (o->ch[]==NULL){
printf("%d\n",o->id);
return o->v;
}
return find_min(o->ch[]);
} int main()
{
//freopen("de.txt","r",stdin);
Node *root=NULL;
int op;
while (~scanf("%d",&op)){
if (op==) break;
if (op==){
int id,v;
scanf("%d%d",&id,&v);
//printf("ins %d %d \n",id,v);
insert(root,v,id);
}
else if (op==){
if (root==NULL) {
printf("0\n");
continue;
}
int v = find_max(root);
remove(root,v);
}
else if (op==){
if (root==NULL) {
printf("0\n");
continue;
}
int v = find_min(root);
remove(root,v);
}
}
return ;
}
POJ 3481 Double Queue (treap模板)的更多相关文章
- POJ 3481 Double Queue STLmap和set新学到的一点用法
2013-08-08 POJ 3481 Double Queue 这个题应该是STL里较简单的吧,用平衡二叉树也可以做,但是自己掌握不够- -,开始想用两个优先队列,一个从大到小,一个从小到大,可是 ...
- POJ 3481 Double Queue(Treap模板题)
Double Queue Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 15786 Accepted: 6998 Des ...
- POJ 3481 Double Queue(STL)
题意 模拟银行的排队系统 有三种操作 1-加入优先级为p 编号为k的人到队列 2-服务当前优先级最大的 3-服务当前优先级最小的 0-退出系统 能够用stl中的map 由于map本身 ...
- POJ 3481 Double Queue(set实现)
Double Queue The new founded Balkan Investment Group Bank (BIG-Bank) opened a new office in Buchares ...
- POJ 3481 Double Queue
平衡树.. 熟悉些fhq-Treap,为啥我在poj读入优化不能用啊 #include <iostream> #include <cstdio> #include <ct ...
- poj 3841 Double Queue (AVL树入门)
/****************************************************************** 题目: Double Queue(poj 3481) 链接: h ...
- POJ-3481 Double Queue,Treap树和set花式水过!
Double Queue 本打算学二叉树,单纯的二叉树感觉也就那几种遍历了, 无意中看到了这个题,然后就 ...
- BZOJ 1588: Treap 模板
1588: [HNOI2002]营业额统计 Time Limit: 5 Sec Memory Limit: 162 MBSubmit: 12171 Solved: 4352 Description ...
- hdu 1908 Double Queue
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1908 Double Queue Description The new founded Balkan ...
随机推荐
- read_ila
close all; clc; %clear; %点数 %point=40960; fft_point=200; show_point=200; end_point=1024; FS=30.72;%% ...
- 124、TensorFlow替换函数
# tf.device给你了很多可伸缩性在TensorFlow的计算图中选择放置你的单独的操作 # 在许多的情况下,有很多启发可以工作的很好 # 例如tf.train.replica_device_s ...
- LinkedList 源码解读
LinkedList 源码解读 基于jdk1.7.0_80 public class LinkedList<E> extends AbstractSequentialList<E&g ...
- Oracle11g安装步骤
plsql安装等:https://blog.csdn.net/li66934791/article/details/83856225 https://www.cnblogs.com/gaoz ...
- sourcetree for mac 使用
1.sourceTree clone 仓库 打开sourceTree, 点击 新仓库(1) -> 从url克隆(2), 如下图 如下图所示, 粘贴源url路径, 自动补全或者手动选择目标路径和名 ...
- 使用多线程开启OCR
需求:经过opencv 或者其他算法对一张图片里面的文字内容进行切割,获取到切割内容的坐标信息,再使用ocr进行识别.一张一张识别太慢了,我们可以开启多线程识别.代码如下 threads = [] f ...
- Linux libOpenThreads库链接冲突错误
最近在linux 上安装了3.7.0版本的OpenSceneGraph,而在安装之前没有完全卸载之前安装的3.6.3版本,导致在编译程序链接时出现库引用冲突,在便以后出现以下警告信息: /usr/bi ...
- PowerShell 远程执行命令
PowerShell 远程执行命令 最近在做一些自动化的测试工作,在代码实现的过程中需要远程启动/关闭一些服务或者测试机. 我首先想到的是建立一个website,通过网站对一些服务进行操作,但是这样感 ...
- MyEclipse下Junit报错"The input type of the launch configuration"
MyEclipse下Junit运行测试用例的时候报错: "The input type of the launch configuration does not exist" 原因 ...
- mysql中插入中文时显示乱码
在插入mysql的时候参数是中文的时候显示的是???乱码,这个是由于没有配置字符编码引起的 只要在SqlMapconfig.xml文件中加上<property name="url&qu ...