treap树---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:
| 0 | The system needs to stop serving |
| 1 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的更多相关文章
- POJ-3481 Double Queue,Treap树和set花式水过!
Double Queue 本打算学二叉树,单纯的二叉树感觉也就那几种遍历了, 无意中看到了这个题,然后就 ...
- poj 3841 Double Queue (AVL树入门)
/****************************************************************** 题目: Double Queue(poj 3481) 链接: h ...
- POJ 3481 Double Queue(Treap模板题)
Double Queue Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 15786 Accepted: 6998 Des ...
- hdu 1908 Double Queue
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1908 Double Queue Description The new founded Balkan ...
- Treap树理解
title: Treap树理解 comments: true date: 2016-10-06 07:57:37 categories: 算法 tags: Treap树 树 Treap树理解 简介 随 ...
- bzoj2141 树状数组套Treap树
题目大意是在能够改变两个数的位置的情况下计算逆序对数 这因为是动态记录逆序对 本来单纯逆序对只要用树状数组计算即可,但这里因为更新,所以利用TReap树的删点和增加点来进行更新 大致是把每个树状数组所 ...
- treap树模板
///treap树模板 typedef struct Node ///节点的结构体 { Node *l,*r; int val,pri; ///节点的值和优先级 int sz; ///节点子树的节点数 ...
- poj 2761 Feed the dogs (treap树)
/************************************************************* 题目: Feed the dogs(poj 2761) 链接: http: ...
- treap树---营业额统计
台州学院 2924 描述 Tiger最近被公司升任为营业部经理,他上任后接受公司交给的第一项任务便是统计并分析公司成立以来的营业情况.Tiger拿出了公司的账本,账本上记录了公司成立以来每天的营业额 ...
随机推荐
- Unity3D]引擎崩溃、异常、警告、BUG与提示总结及解决方法
此贴会持续更新,都是项目中常会遇到的问题,总结成贴,提醒自己和方便日后检查,也能帮到有需要的同学. 若各位有啥好BUG好异常好警告好崩溃可以分享的话,请多多指教.xuzhiping7#qq.com. ...
- 在Win8.1系统下如何安装运行SQL Server 2005 (以及安装SQL Server 2005 Express打补丁)
在Win8.1系统下SQL Server 2005 安装失败怎么办? 需要替换两个文件及打sqlserver sp4补丁. 以下是操作过程. 按正常情况,在Win8/Win8.1系统下安装微软的SQL ...
- mac或linux下xampp的mysql配置
在mac 下安装好xampp后,需要在终端命令行操作时,比如输入:mysql -u root -p,未正确配置前不会出现想要的输入密码提示,而是会提示: command not found 原来当你输 ...
- Spring4 MVC Hibernate4集成 Annotation
Spring4 MVC Hibernate4集成 Annotation 一.本文所用环境 二.工程目录 三.Maven添加依赖 四.新建数据库表 五.配置文件 六.Model层 七.DAO层 八.Se ...
- 点击弹出 +1放大效果 -- jQuery插件
20140110更新: <!doctype html> <html> <head> <meta charset="UTF-8"> & ...
- 调用 webapi的put和delete 报"Method Not Allowed" 405 错误。
修改引用到webapi的Dll文件对应的项目的web.config 选择生成读写方法webapi会生成四个读写的方法(CRUD),两个获取数据的.一个更新.一个删除,默认情况下更新和删除是不对外开外的 ...
- XCode使用自带SVN,SVN命令
转载http://blog.sina.com.cn/s/blog_68661bd80101phpy.html 这两天响应老板要求,把所有代码放到公司的SVN服务器上,按照我的想法肯定是就苹果组建一个服 ...
- easyui plugin——etreegrid:CRUD Treegrid
昨天写了一个ko+easyui的同样的实现,感觉写的太乱,用起来十分麻烦,于是今天照着edatagrid,写了一个etreegrid,这样再用ko绑定就方便多了. 使用很简单,$(tableId).e ...
- MyBatis+MySQL 返回插入的主键ID
需求:使用MyBatis往MySQL数据库中插入一条记录后,需要返回该条记录的自增主键值. 方法:在mapper中指定keyProperty属性,示例如下: <insert id="i ...
- 彻底解决Google浏览器CSS居中问题
div做的界面时,又出现CSS hack(CSS兼容浏览器问题)在IE内核浏览器或者firefox浏览器中都能居中,没有居中的可以用其特殊标签来设定居中,如下划线 _ IE6优先识别,!importa ...