hdoj 5249 KPI(treap)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5249
思路分析:使用queue记录管道中的值并使用treap能够查询第K大的功能查询第floor(m/2)+1大的数值;
对于in value操作,将value插入queue中和treap中;对于out操作,在treap中删除queue中队头元素,并在queue中使队头元素出队;
对于query操作,因为tail – head的值即为管道中的值的个数,使用treap查询第floor((tail – head) / 2) + 1大的元素即可;
代码如下:
#include <cstdio>
#include <ctime>
#include <cstring>
#include <iostream>
using namespace std; const int MAX_N = + ;
int queue[MAX_N]; struct Node{
Node *ch[];
int value, key;
int size;
int cmp(int x) const{
if (x == value) return -;
return x < value ? : ;
}
void Maintain(){
size = ;
if (ch[] != NULL) size += ch[]->size;
if (ch[] != NULL) size += ch[]->size;
}
}; void Rotate(Node *&o, int d){
Node *k = o->ch[d ^ ];
o->ch[d ^ ] = k->ch[d];
k->ch[d] = o;
o->Maintain();
k->Maintain();
o = k;
} void Insert(Node *&o, int x){
if (o == NULL){
o = new Node();
o->ch[] = o->ch[] = NULL;
o->value = x;
o->key = rand();
}
else{
int d = (x < (o->value) ? : );
Insert(o->ch[d], x);
if (o->ch[d]->key > o->key)
Rotate(o, d ^ );
}
o->Maintain();
} 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[]->key > o->ch[]->key ? : ); 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);
if (o != NULL)
o->Maintain( );
} int Find(Node *o, int x){
while (o != NULL){
int d = o->cmp(x); if (d == -) return ;
else o = o->ch[d];
}
return ;
} int FindKth(Node *o, int k){
int l_size = (o->ch[] == NULL ? : o->ch[]->size);
if (k == l_size + )
return o->value;
else if (k <= l_size)
return FindKth(o->ch[], k);
else
return FindKth(o->ch[], k - l_size - );
} void MakeEmpty(Node *root){
if (root == NULL)
return;
if (root->ch[])
MakeEmpty(root->ch[]);
if (root->ch[])
MakeEmpty(root->ch[]);
delete root;
} int main(){
int n, case_id = , value; srand();
while (scanf("%d", &n) != EOF){
Node *root = NULL;
int head = , tail = , ans;
char str[]; printf("Case #%d:\n", ++case_id);
for (int i = ; i < n; ++i){
scanf("%s", str);
if (str[] == 'i'){
scanf("%d", &value);
queue[tail++] = value;
Insert(root, value);
}
else if (str[] == 'o')
Remove(root, queue[head++]);
else{
ans = FindKth(root, (tail - head) / + );
printf("%d\n", ans);
}
}
MakeEmpty(root);
}
return ;
}
hdoj 5249 KPI(treap)的更多相关文章
- hdu 5249 KPI
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5249 KPI Description 你工作以后, KPI 就是你的全部了. 我开发了一个服务,取得了 ...
- HDU 5249:KPI(权值线段树)
KPI Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Problem Desc ...
- 2015年百度之星初赛(1) --- D KPI
KPI Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
- fhq treap最终模板
新学习了fhq treap,厉害了 先贴个神犇的版, from memphis /* Treap[Merge,Split] by Memphis */ #include<cstdio> # ...
- 大数据慎行,数据管理要落实到KPI
近年来,"大数据"一词被IT和互联网行业广泛提及,但真正落到实处的案例没有多少,大数据量支撑.数据挖掘技术.非结构化数据是阻碍的主要原因.大多数企业的信息化并没有达到到成熟水平,关 ...
- HDOJ 1009. Fat Mouse' Trade 贪心 结构体排序
FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- HDOJ 2317. Nasty Hacks 模拟水题
Nasty Hacks Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tota ...
- HDOJ 1326. Box of Bricks 纯水题
Box of Bricks Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) To ...
- BZOJ 1691: [Usaco2007 Dec]挑剔的美食家 [treap 贪心]
1691: [Usaco2007 Dec]挑剔的美食家 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 786 Solved: 391[Submit][S ...
随机推荐
- [vc]如何对radio按钮分组
如何使用多组? 多组和一组是一样的使用,只要搞清楚哪个是哪一组的就行了.再为对话框添加Radio3和Radio4.很简单,先为这些RadioButton排个顺序,就是排列他们的TABORDER.在对话 ...
- 小鱼提问1 类中嵌套public修饰的枚举,外部访问的时候却只能Class.Enum这样访问,这是为何?
/// <summary> /// 常量等定义 /// </summary> public class General { /// <summary> /// 文件 ...
- classpath的总结
转自:http://blog.csdn.net/javaloveiphone/article/details/51994268 版权声明:本文为博主原创文章,未经博主允许不得转载. 1.src不是 ...
- JavaScript 网页链接调用Android程序
如何让网页链接实现启动Android的应用,网上有说重写WebView相关的shouldOverrideUrlLoading方法,但是这种理论上能实现,因为你的网页不是仅仅被你自己的webview来浏 ...
- junit的安装和使用
一.junit的安装: junit-4.11.jar: http://www.java2s.com/Code/Jar/j/Downloadjunit411jar.htm hamcrest-core.j ...
- grunt api 文档
Grunt docs Grunt和 Grunt 插件是通过 npm 安装并管理的,npm是 Node.js 的包管理器. 安装 grunt-cli npm install grunt-cli -g 注 ...
- oracle在一个字符串中查找某个字符出现过几次
SELECT LENGTH(REGEXP_REPLACE(REPLACE('123,45,6,5', ',', '@'), '[^@]+', '')) COUNT FROM DUAL; 返回结果为 ...
- hdu4491 Windmill Animation (几何)
Windmill Animation Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- java 循环制作三角形
package hello; public class Sanjiao { public static void main(String[]args){ for(int i=1;i<5;i++) ...
- HDU 1002 A + B Problem II(大整数相加)
A + B Problem II Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u De ...