题目链接: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)的更多相关文章

  1. hdu 5249 KPI

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5249 KPI Description 你工作以后, KPI 就是你的全部了. 我开发了一个服务,取得了 ...

  2. HDU 5249:KPI(权值线段树)

    KPI Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Problem Desc ...

  3. 2015年百度之星初赛(1) --- D KPI

    KPI Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

  4. fhq treap最终模板

    新学习了fhq treap,厉害了 先贴个神犇的版, from memphis /* Treap[Merge,Split] by Memphis */ #include<cstdio> # ...

  5. 大数据慎行,数据管理要落实到KPI

    近年来,"大数据"一词被IT和互联网行业广泛提及,但真正落到实处的案例没有多少,大数据量支撑.数据挖掘技术.非结构化数据是阻碍的主要原因.大多数企业的信息化并没有达到到成熟水平,关 ...

  6. HDOJ 1009. Fat Mouse' Trade 贪心 结构体排序

    FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  7. HDOJ 2317. Nasty Hacks 模拟水题

    Nasty Hacks Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Tota ...

  8. HDOJ 1326. Box of Bricks 纯水题

    Box of Bricks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) To ...

  9. BZOJ 1691: [Usaco2007 Dec]挑剔的美食家 [treap 贪心]

    1691: [Usaco2007 Dec]挑剔的美食家 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 786  Solved: 391[Submit][S ...

随机推荐

  1. C#委托的简单剖析

    为什么在Button1的Click事件发生之后,button1_Click方法就会被调用呢? 实际上,在我们双击Button1的时候,IDE自动的添加了一段代码,该段代码位于“Form1.Design ...

  2. SQL Server JDBC驱动中sqljdbc和sqljdbc4区别

    为了支持向后兼容以及可能的升级方案,JDBC Driver 2.0 在每个安装包中都包括 2 个 JAR 类库:sqljdbc.jar 和 sqljdbc4.jar. qljdbc.jar 类库提供对 ...

  3. 戏说Java多线程

    戏说Java多线程. package com.cmp.chengfu.app.jdk6.desktop; import java.awt.Desktop; import java.io.File; i ...

  4. R与数据分析旧笔记(四)画地图练习

    > library(maps) > library(geosphere) 载入需要的程辑包:sp > map("state")#画美国地图 > map(&q ...

  5. 在CentOS 7 / Gnome 3 双屏时设置主屏

    在Windows中设置扩展显示器为主屏的方式非常清楚,但在Linux中就不是那么明显了,下面介绍如何完成这个设置 ------------------------------------------- ...

  6. codeforces 416B. Appleman and Tree 树形dp

    题目链接 Fill a DP table such as the following bottom-up: DP[v][0] = the number of ways that the subtree ...

  7. idea修改文件名后出现main method should be static错误

    1.确保你有main方法 2.别忘了main方法里还有参数: String[] args

  8. JS笔记 入门第三

    认识DOM 文档对象模型DOM(Document Object Model)定义访问和处理HTML文档的标准方法.DOM 将HTML文档呈现为带有元素.属性和文本的树结构(节点树) 把上面的代码进行分 ...

  9. Linux 下修改Tomcat使用的JVM内存大小

    我的服务器的配置: # OS specific support.  $var _must_ be set to either true or false. JAVA_OPTS="-Xms10 ...

  10. [VC6 console]调用API获取手机归属地

    为了完成作业,就偷个懒糊了个获取手机归属地的程序,.我原本写的是MFC版本的,但是由于MFC的代码不是很通用,加上我没有学MFC的时候看别人MFC代码只能干瞪眼,看不懂,所以便改成控制台版本的了.但这 ...