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 ...
随机推荐
- iOS 点转成字符串,再字符串转换成点
CGPointFromString(<#NSString *string#>) NSStringFromCGPoint(<#CGPoint point#>)
- 为IE6-7间接支持:before和:after伪类
:before和:after我们经常会用到,特别是在做移动端页面时,利用它制作文字前后的ICON.图片的垂直居中之类的非常方便且代码简洁(当然,功能远比这些要多的多...). 可是在PC端,由于现在还 ...
- AutoCompleteTextView 自动提示
在输入框中输入我们想要输入的信息就会出现其他与其相关的提示信息,这种效果在Android中是用AutoCompleteTextView实现的. public class MainActivity ex ...
- 【转】Virtualbox虚拟机配置安装CentOS 6.5图文教程
http://www.111cn.net/sys/CentOS/61709.htm 什么是Virtualbox? VirtualBox 是一款开源虚拟机软件(注:跟vmware差不多).Virtual ...
- Qt窗口屏幕居中显示(有专门的QDesktopWidget,先计算后显示)
窗口的屏幕居中显示问题,在各开发工具中原理相同,首先使用特定的方法得到显示屏幕的宽度和高度,再根据窗口本身的宽度和高度计算出窗口的左上角坐标位置. Qt中可以采用两种方法达到窗口的屏幕居中显示: 方法 ...
- Android程序报错 Connection refused 处理
在用Android测试JSON数据解析的时候,报了这样一个异常: java.net.ConnectException: localhost/ - Connection refused 原来模拟器默认把 ...
- Android开发中如何解决加载大图片时内存溢出的问题
Android开发中如何解决加载大图片时内存溢出的问题 在Android开发过程中,我们经常会遇到加载的图片过大导致内存溢出的问题,其实类似这样的问题已经屡见不鲜了,下面将一些好的解决方案分享给 ...
- POJ 3261 Milk Patterns(后缀数组+二分答案)
[题目链接] http://poj.org/problem?id=3261 [题目大意] 求最长可允许重叠的出现次数不小于k的子串. [题解] 对原串做一遍后缀数组,二分子串长度x,将前缀相同长度超过 ...
- Course(简单的字符串处理问题)
Course 时间限制:1000 ms | 内存限制:65535 KB [问题描述] There is such a policy in Sichuan University that if yo ...
- C(n+m,m) mod p的一类算法
Lucas定理 A.B是非负整数,p是质数.AB写成p进制:A=a[n]a[n-1]...a[0],B=b[n]b[n-1]...b[0]. 则组合数C(A,B)与C(a[n],b[n])*C(a[n ...