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 ...
随机推荐
- idea15破解
注册方法: 注册码可以沿用14的,只是在 注册时选择 License server ,填 http://idea.lanyus.com ,然后点击 OK 14的话,网上可以找到一个,根据你的用户名 ...
- c#Ulong用一个高位Uint和低位Uint表示
有时候考虑到平台之间的通用性,可能把一个Ulong拆分成2个Uint来进行各平台之间的通讯,当时转换的时候有点头晕,对与或预算不是很熟悉,不过还是花了半小时弄出来了,代码: //ulong的最大值2^ ...
- B - Moving Tables
B - Moving Tables Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u S ...
- Mysql 如何做双机热备和负载均衡 (方法二)
先简要介绍一下mysql双向热备:mysql从3.23.15版本以后提供数据库复制功能.利用该功能可以实现两个数据库同步,主从模式(A->B),互相备份模式(A<=>B)的功能. m ...
- Android 修改host文件的3种方法
Android修改hosts文件的方法介绍 本文介绍三种Android手机修改hosts文 件的方法,但修改hosts文件一定要谨慎:Android手机hosts文件的换行符必须是n而不是window ...
- 3种方式实现可滑动的Tab
1. 第一种,使用 TabHost + ViewPager 实现 该方法会有一个Bug,当设置tabHost.setCurrentTab()为0时,ViewPager不显示(准确的说是加载),只有点击 ...
- .net mvc笔记2_Essential C# Features
Essential C# Features 1.Using Automatically Implemented Properties public class Product { private st ...
- MySQLD 配置
http://blog.163.com/sir_876/blog/static/11705223201372710303382/ http://www.kankanews.com/ICkengine/ ...
- POJ 3581 Sequence(后缀数组)
[题目链接] http://poj.org/problem?id=3581 [题目大意] 给出一个数列,将这个数列分成三段,每段分别翻转,使得其字典序最小,输出翻转后的数列. [题解] 首先,第一个翻 ...
- Linux操作系统定时任务系统Cron入门、PHP计划任务以及rpc示例
一.简单介绍 1.cron是一个linux下的定时执行工具,可以在无需人工干预的情况下运行作业.由于Cron 是Linux的内置服务,但它不自动起来,可以用以下的方法启动.关闭这个服务: servic ...