HDU   1908

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
KP 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 identifierK 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、2和3。1代表加入一个人,紧接着输入这个人的编号与优先级;2表示在加入的人中找到优先级最大的人,然后输出他的编号并从加入的人里剔除他;3表示在加入的人中找到优先级最小的人,然后输出他的编号并从加入的人里剔除他;
思路:使用treap树插入加入的人的编号与优先级,利用二叉树左子树小右子树大的性质,可知最左边的节点的优先级最小,最右边的节点的优先级最大;
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
struct data
{
int l,r,v,vo;
int rnd;
}tr[];
int size,root,ans2;///定义全局整型变量默认初值为0; void rturn(int &k)
{
int t=tr[k].l;
tr[k].l=tr[t].r;
tr[t].r=k;
k=t;
} void lturn(int &k)
{
int t=tr[k].r;
tr[k].r=tr[t].l;
tr[t].l=k;
k=t;
} void insert(int &k,int x,int xo)
{
if(k==)
{
size++;///记录已经使用的结构体数目;
k=size;
tr[k].v=x;
tr[k].vo=xo;
tr[k].rnd=rand();
return;
}
if(x>tr[k].v)
{
insert(tr[k].r,x,xo);
if(tr[tr[k].r].rnd<tr[k].rnd)
lturn(k);
}
else
{
insert(tr[k].l,x,xo);
if(tr[tr[k].l].rnd<tr[k].rnd)
rturn(k);
}
} void search_min(int &k)///最小值一定在最左的节点上;
{
if(tr[k].l==)
{
ans2=tr[k].vo;
k=tr[k].r;
return ;
}
search_min(tr[k].l);
} void search_max(int &k)///最大值一定在最右的节点上;
{
if(tr[k].r==)
{
ans2=tr[k].vo;
k=tr[k].l;
return ;
}
search_max(tr[k].r);
} int main()
{
int x,k,p;
while(scanf("%d",&x)!=EOF&&x)
{
ans2=;
if(x==)
{
scanf("%d%d",&k,&p);
insert(root,p,k);
}
else if(x==)
{
if(root)
search_max(root);
printf("%d\n",ans2);
}
else if(x==)
{
if(root)
search_min(root);
printf("%d\n",ans2);
}
}
return ;
}

treap树---Double Queue的更多相关文章

  1. POJ-3481 Double Queue,Treap树和set花式水过!

                                                    Double Queue 本打算学二叉树,单纯的二叉树感觉也就那几种遍历了, 无意中看到了这个题,然后就 ...

  2. poj 3841 Double Queue (AVL树入门)

    /****************************************************************** 题目: Double Queue(poj 3481) 链接: h ...

  3. POJ 3481 Double Queue(Treap模板题)

    Double Queue Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 15786   Accepted: 6998 Des ...

  4. hdu 1908 Double Queue

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1908 Double Queue Description The new founded Balkan ...

  5. Treap树理解

    title: Treap树理解 comments: true date: 2016-10-06 07:57:37 categories: 算法 tags: Treap树 树 Treap树理解 简介 随 ...

  6. bzoj2141 树状数组套Treap树

    题目大意是在能够改变两个数的位置的情况下计算逆序对数 这因为是动态记录逆序对 本来单纯逆序对只要用树状数组计算即可,但这里因为更新,所以利用TReap树的删点和增加点来进行更新 大致是把每个树状数组所 ...

  7. treap树模板

    ///treap树模板 typedef struct Node ///节点的结构体 { Node *l,*r; int val,pri; ///节点的值和优先级 int sz; ///节点子树的节点数 ...

  8. poj 2761 Feed the dogs (treap树)

    /************************************************************* 题目: Feed the dogs(poj 2761) 链接: http: ...

  9. treap树---营业额统计

    台州学院  2924 描述 Tiger最近被公司升任为营业部经理,他上任后接受公司交给的第一项任务便是统计并分析公司成立以来的营业情况.Tiger拿出了公司的账本,账本上记录了公司成立以来每天的营业额 ...

随机推荐

  1. 增大VM下linux的根目录空间

    增大VM下linux的根目录空间   用的太久,发现VM下的系统空间不足.简单的方法是,分一个新硬盘,挂载到根目录下.    下面是直接增大根目录下空间:  1. 增大vm下的磁盘大小, VM -&g ...

  2. android中无限循环滑动的gallery实例

    android中无限循环滑动的gallery实例 1.点击图片有变暗的效果,使用imageview.setAlpha(),并且添加ontouchListener public void init() ...

  3. Xenia and Divisors

    Xenia and Divisors time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  4. 【转】ContextMenuStrip菜单应用

    测试可用的代码: #region 右键快捷菜单单击事件 private void contextMenuStrip1_ItemClick(object sender, EventArgs e) { T ...

  5. google chrome set

    "D:\Program Files\Google\Chrome\Application\chrome_bk.exe" --start-maximized --user-data-d ...

  6. Android之Handler源码深入分析

    闲着没事,就来看看源码,看看源码的各种原理,会用只是简单的,知道为什么才是最牛逼的. Handler源码分析那,从使用的步骤来边用边分析: 1.创建一个Handler对象:new Handler(ge ...

  7. [OpenCV] Image Processing - Fuzzy Set

    使用模糊技术进行 (灰度变换Grayscale Transform) 和 (空间滤波Spatial Filtering) 模糊集合为处理不严密信息提供了一种形式. 首先,需要将输入量折算为隶属度,这个 ...

  8. 关于VR技术和未来发展---转

    原文地址:http://mp.weixin.qq.com/s?__biz=MzA4MTIwNTczMQ==&mid=2651345594&idx=3&sn=2741ab7321 ...

  9. 移动端 ios 长按复制兼容方案

    移动端页面,需要复制一段文字码. 在ios中,长按文字区域,默认选中的范围,超出了我长按的文字区域, 把上面的图片和下面的另一个div的文字也给我包含进来了,并不是我想要的! 举个例子: 如下图: 1 ...

  10. UITextField-secureTextEntry

    1.UITextFiled的密文输入   secureTextEntry  安全文本输入  secure:安全  Entry:入口