Black Box(POJ 1442·TREAP实现)
传送门:http://poj.org/problem?id=1442
Time Limit: 1000MS | Memory Limit: 10000K | |
Description
ADD (x): put element x into Black Box;
GET: increase i by 1 and give an i-minimum out of all integers containing in the Black Box. Keep in mind that i-minimum is a number located at i-th place after Black Box elements sorting by non- descending.
Let us examine a possible sequence of 11 transactions:
Example 1
N Transaction i Black Box contents after transaction Answer (elements are arranged by non-descending) 1 ADD(3) 0 3 2 GET 1 3 3 3 ADD(1) 1 1, 3 4 GET 2 1, 3 3 5 ADD(-4) 2 -4, 1, 3 6 ADD(2) 2 -4, 1, 2, 3 7 ADD(8) 2 -4, 1, 2, 3, 8 8 ADD(-1000) 2 -1000, -4, 1, 2, 3, 8 9 GET 3 -1000, -4, 1, 2, 3, 8 1 10 GET 4 -1000, -4, 1, 2, 3, 8 2 11 ADD(2) 4 -1000, -4, 1, 2, 2, 3, 8
It is required to work out an efficient algorithm which treats a given sequence of transactions. The maximum number of ADD and GET transactions: 30000 of each type.
Let us describe the sequence of transactions by two integer arrays:
1. A(1), A(2), ..., A(M): a sequence of elements which are being included into Black Box. A values are integers not exceeding 2 000 000 000 by their absolute value, M <= 30000. For the Example we have A=(3, 1, -4, 2, 8, -1000, 2).
2. u(1), u(2), ..., u(N): a sequence setting a number of elements which are being included into Black Box at the moment of first, second, ... and N-transaction GET. For the Example we have u=(1, 2, 6, 6).
The Black Box algorithm supposes that natural number sequence u(1), u(2), ..., u(N) is sorted in non-descending order, N <= M and for each p (1 <= p <= N) an inequality p <= u(p) <= M is valid. It follows from the fact that for the p-element of our u sequence we perform a GET transaction giving p-minimum number from our A(1), A(2), ..., A(u(p)) sequence.
Input
Output
Sample Input
7 4 3 1 -4 2 8 -1000 2 1 2 6 6
Sample Output
3 3 1 2
Source
1 #include<set> 2 #include<ctime> 3 #include<queue> 4 #include<cstdio> 5 #include<cstdlib> 6 #include<cstring> 7 #include<iostream> 8 #include<algorithm> 9 using namespace std; 10 const int N = 100100; 11 #define L(i) (T[i].s[0]) 12 #define R(i) (T[i].s[1]) 13 #define For(i,n) for(int i=1;i<=n;i++) 14 #define Rep(i,l,r) for(int i=l;i<=r;i++) 15 16 struct treap{ 17 int size,s[2],v,pri; 18 void Sets(int x,int y){ 19 size = 1;v = x;pri = y; 20 } 21 }T[N]; 22 23 int n,m,A[N],size,Lim,now,level = 0; 24 int tot,root; 25 int read(){ 26 char ch = getchar(); int num = 0 , q = 1; 27 while(ch>'9'||ch<'0'){ 28 if(ch=='-') q = -1; 29 ch = getchar(); 30 } 31 while(ch>='0'&&ch<='9'){ 32 num = num * 10 + ch - '0'; 33 ch = getchar(); 34 } 35 return num * q; 36 } 37 38 void Update(int i){ 39 T[i].size = T[L(i)].size + T[R(i)].size + 1; 40 } 41 42 void Rot(int &y,int f){ 43 int x = T[y].s[!f]; 44 T[y].s[!f] = T[x].s[f]; 45 T[x].s[f] = y; 46 Update(y);Update(x); 47 y = x; 48 } 49 50 void Insert(int &i,int val){ 51 if(!i){ 52 T[i=++tot].Sets(val,rand()); 53 return; 54 } 55 int f = T[i].v > val; 56 Insert(T[i].s[!f],val); 57 if(T[T[i].s[!f]].pri > T[i].pri) Rot(i,f); 58 else Update(i); 59 } 60 61 int Rank(int i,int kth){ 62 if(T[L(i)].size + 1 == kth) return i; 63 else if(T[L(i)].size >=kth) return Rank(L(i),kth); 64 else return Rank(R(i),kth - T[L(i)].size - 1); 65 } 66 67 int main(){ 68 srand(time(NULL)); 69 n = read(); m = read(); 70 For(i,n) A[i] = read(); 71 For(i,m) { 72 Lim = read(); 73 Rep(i,now+1,Lim) Insert(root,A[i]); now = Lim; 74 level++;printf("%d\n",T[Rank(root,level)].v); 75 } 76 return 0; 77 }
转载于:https://www.cnblogs.com/zjdx1998/p/3885542.html
Black Box(POJ 1442·TREAP实现)相关推荐
- POJ 1442 Black Box(大小堆,求第K小的元素)
文章目录 1. 题目链接 2. 题目解读 3. 代码 3.1 Runtime Error 代码 1. 题目链接 http://poj.org/problem?id=1442 2. 题目解读 可以利用大 ...
- [POJ 1442]Black Box
Description 囧囧有个囧盒,可以处理一些很囧的事情.这个囧盒一开始是空的,可以处理2种操作: Add(X):将元素X放入囧盒. Get:第I次调用Get的时候,输出囧盒里面第I小的数.下面是 ...
- POJ 3481 treap
这是利用treap写的二叉排序树,只要理解其中旋转能够改变树的左右子树平衡度,即高度之差,差不多就能掌握treap树的要领了. 相对于其他高级BST,treap树实现应该算最简单了,利用的是随机树产生 ...
- 三大平衡树(Treap + Splay + SBT)总结+模板
Treap树 核心是 利用随机数的二叉排序树的各种操作复杂度平均为O(lgn) Treap模板: #include <cstdio> #include <cstring> #i ...
- 《数据结构与算法之美》学习汇总
此篇文章是对自己学习这门课程的一个总结和课后的一些练习,做一个汇总,希望对大家有帮助.本人是半路程序员,2018年2月开始学习C++的,下面的代码基本都是C++11版本的,代码有错误的地方请不吝留言赐 ...
- Treap原理和实现方法
Treap是一棵二叉搜索树,只是每个节点多了一个优先级fix,对于每个节点,该节点的优先级小于等于其所有孩子的优先级. 当然,引入优先级fix的目的就是防止BST退化成一条链,从而影响查找效率. 所以 ...
- POJ前面的题目算法思路【转】
1000 A+B Problem 送分题 49% 2005-5-7 1001 Exponentiation 高精度 85% 2005-5-7 1002 487-3279 n/a 90% 2005-5- ...
- poj题目详细分类及算法推荐题目
DP: 1011 NTA 简单题 1013 Great Equipment 简单题 1024 Calendar Game 简单题 ...
- ACM POJ 题目分类(完整整理版本)
DP: 1011 NTA 简单题 1013 Great Equipment 简单题 1024 Calendar Game 简单题 ...
最新文章
- java 中格式化数字
- 机器学习总结(17)-XGBoost
- f12获取网页文本_8招教你快速搞定网页内容禁止复制粘贴,想怎么复制就怎么复制...
- 计组之存储系统:7、Cache替换算法(随机算法RAND、先进先出算法FIFO、近期最少使用LRU、最近不经常使用LFU)
- 京东健康CEO辛利军:感谢刘强东的战略格局和超前眼光
- mooc上c语言怎么改作业,C语言的编写、运行和修复代码
- CSRF verification failed. Request aborted.的解决办法
- Android爬坑之旅之FileProvider(Failed to find configured root that contains)
- 【学生网页设计作业源码】基于HTML+CSS+JavaScript简单的大学生书店(13个页面) 二手书店电子商务网站模板源码
- 网页编程html报错502,Nginx将不会使用自定义502错误页面
- 鹏业安装算量软件V8.0.0.92升级内容
- MinDoc v0.4 发布 轻量级文档在线管理系统
- 我在名牌大学毕业后的经历——曾经努力过,就不会后悔
- 2、yum和epel是什么
- vb安装过程中 ntvdm.exe[9696]中发生未处理的win32异常 vb代写
- Windows上python读取grib2文件(不用Linux)
- 机器学习之决策树算法前期(创建、分类及展示)
- html 醒目的样式,创意醒目门牌设计欣赏
- oracle删除两个月以前的数据,Oracle恢复删除数据
- 小程序-demo:知乎日报
热门文章
- Jmeter(二)关联
- Android应用开发提高篇(2)-----文本朗读TTS(TextToSpeech)
- SpringBoot开发Restful接口
- VBA打开TXT类文件读写相关操作代码
- 广发银行创新“智慧金融”打造“智慧城市”
- DefaultListableBeanFactory - Overriding bean defin
- java八大基本类型介绍
- 精进不休 .NET 4.0 (2) - asp.net 4.0 新特性之url路由
- asp.net Core 中间件Hello world
- 理解Python的With语句