hdu 5249 KPI
题目连接
http://acm.hdu.edu.cn/showproblem.php?pid=5249
KPI
Description
你工作以后, KPI 就是你的全部了. 我开发了一个服务,取得了很大的知名度。数十亿的请求被推到一个大管道后同时服务从管头拉取请求。让我们来定义每个请求都有一个重要值。我的KPI是由当前管道内请求的重要值的中间值来计算。现在给你服务记录,有时我想知道当前管道内请求的重要值得中间值。
Input
有大约100组数据。
每组数据第一行有一个$n(1 \leq n \leq 10000)$,代表服务记录数。
接下来有n行,每一行有3种形式
"in x": 代表重要值为$x(0 \leq x \leq 10^9)$的请求被推进管道。
"out": 代表服务拉取了管道头部的请求。
"query: 代表我想知道当前管道内请求重要值的中间值. 那就是说,如果当前管道内有m条请求, 我想知道,升序排序后第$floor(m/2)+1_{th}$ 条请求的重要值.
为了让题目简单,所有的x都不同,并且如果管道内没有值,就不会有"out"和"query"操作。
Output
对于每组数据,先输出一行
Case #i:
然后每一次"query",输出当前管道内重要值的中间值。
Sample Input
6
in 874
query
out
in 24622
in 12194
query
Sample Output
Case #1:
874
24622
红黑树:
#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<queue>
using std::queue;
const int Max_N = ;
struct Node {
int data, s;
bool color;
Node *fa, *ch[];
inline void set(int _v, int i, bool _color, Node *p) {
data = _v, color = _color, s = i;
fa = ch[] = ch[] = p;
}
inline void push_up() {
s = ch[]->s + ch[]->s + ;
}
inline void push_down() {
for (Node *x = this; x->s; x = x->fa) x->s--;
}
};
struct RedBlackTree {
int top;
Node *root, *null;
Node stack[Max_N], *tail, *store[Max_N];
void init() {
tail = &stack[];
null = tail++;
null->set(, , , NULL);
root = null;
top = ;
}
inline Node *newNode(int v) {
Node *p = null;
if (!top) p = tail++;
else p = store[--top];
p->set(v, , , null);
return p;
}
inline void rotate(Node* &x, bool d) {
Node *y = x->ch[!d];
x->ch[!d] = y->ch[d];
if (y->ch[d] != null) y->ch[d]->fa = x;
y->fa = x->fa;
if (x->fa == null) root = y;
else x->fa->ch[x->fa->ch[] != x] = y;
y->ch[d] = x;
x->fa = y;
y->s = x->s;
x->push_up();
}
inline void insert(int v) {
Node *x = root, *y = null;
while (x->s){
x->s++;
y = x, x = x->ch[v > x->data];
}
x = newNode(v);
if (y != null) y->ch[v > y->data] = x;
else root = x;
x->fa = y;
insert_fix(x);
}
inline void insert_fix(Node* &x) {
while (x->fa->color){
Node *par = x->fa, *Gp = par->fa;
bool d = par == Gp->ch[];
Node *uncle = Gp->ch[d];
if (uncle->color) {
par->color = uncle->color = ;
Gp->color = ;
x = Gp;
} else if (x == par->ch[d]) {
rotate(x = par, !d);
} else {
Gp->color = ;
par->color = ;
rotate(Gp, d);
}
}
root->color = ;
}
inline Node *find(Node *x, int data) {
while (x->s && x->data != data) x = x->ch[x->data < data];
return x;
}
inline void del_fix(Node* &x) {
while (x != root && !x->color) {
bool d = x == x->fa->ch[];
Node *par = x->fa, *sibling = par->ch[d];
if (sibling->color) {
sibling->color = ;
par->color = ;
rotate(x->fa, !d);
sibling = par->ch[d];
} else if (!sibling->ch[]->color && !sibling->ch[]->color) {
sibling->color = , x = par;
} else {
if (!sibling->ch[d]->color) {
sibling->ch[!d]->color = ;
sibling->color = ;
rotate(sibling, d);
sibling = par->ch[d];
}
sibling->color = par->color;
sibling->ch[d]->color = par->color = ;
rotate(par, !d);
break;
}
}
x->color = ;
}
inline void del(int data) {
Node *z = find(root, data);
if (!z->s) return;
Node *y = z, *x = null;
if (z->ch[]->s && z->ch[]->s) {
y = z->ch[];
while (y->ch[]->s) y = y->ch[];
}
x = y->ch[!y->ch[]->s];
x->fa = y->fa;
if (!y->fa->s) root = x;
else y->fa->ch[y->fa->ch[] == y] = x;
if (z != y) z->data = y->data;
y->fa->push_down();
if (!y->color) del_fix(x);
store[top++] = y;
}
inline int kth(int k) {
int t = ;
Node *x = root;
for (; x->s;){
t = x->ch[]->s;
if (k == t + ) break;
else if (k <= t) x = x->ch[];
else k -= t + , x = x->ch[];
}
return x->data;
}
int operator[] (int k) {
return kth(k);
}
}rbt;
int main(){
#ifdef LOCAL
freopen("in.txt", "r", stdin);
freopen("out.txt", "w+", stdout);
#endif
int n, v, c = ;
char buf[];
while (~scanf("%d", &n)) {
rbt.init(); queue<int> q;
printf("Case #%d:\n", c++);
while (n--) {
scanf("%s", buf);
if ('i' == buf[]) {
scanf("%d", &v);
rbt.insert(v), q.push(v);
} else if ('o' == buf[]) {
v = q.front(); q.pop();
rbt.del(v);
} else {
printf("%d\n", rbt[((int)q.size() >> ) + ]);
}
}
}
return ;
}
sb树:
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<queue>
using std::queue;
const int Max_N = ;
struct Node {
int v, s;
Node *ch[];
inline void set(int _v, int _s, Node *p) {
v = _v, s = _s;
ch[] = ch[] = p;
}
inline void push_up() {
s = ch[]->s + ch[]->s + ;
}
inline int cmp(int x) const {
return x == v ? - : x > v;
}
};
struct SizeBalanceTree {
Node stack[Max_N];
Node *root, *null, *tail;
Node *store[Max_N];
int top;
void init() {
tail = &stack[];
null = tail++;
null->set(, , NULL);
root = null;
top = ;
}
inline Node *newNode(int v) {
Node *p = null;
if (top) p = store[--top];
else p = tail++;
p->set(v, , null);
return p;
}
inline void rotate(Node* &x, int d) {
Node *k = x->ch[!d];
x->ch[!d] = k->ch[d];
k->ch[d] = x;
k->s = x->s;
x->push_up();
x = k;
}
inline void Maintain(Node* &x, int d) {
if (x->ch[d] == null) return;
if (x->ch[d]->ch[d]->s > x->ch[!d]->s) {
rotate(x, !d);
} else if (x->ch[d]->ch[!d]->s > x->ch[!d]->s) {
rotate(x->ch[d], d), rotate(x, !d);
} else {
return;
}
Maintain(x, ), Maintain(x, );
}
inline void insert(Node* &x, int v) {
if (x == null) {
x = newNode(v);
return;
} else {
x->s++;
int d = x->cmp(v);
insert(x->ch[d], v);
x->push_up();
Maintain(x, d);
}
}
inline void del(Node* &x, int v) {
if (!x->s) return;
x->s--;
int d = x->cmp(v);
if (- == d) {
if (!x->ch[]->s || !x->ch[]->s) {
store[top++] = x;
x = x->ch[]->s ? x->ch[] : x->ch[];
} else {
Node *ret = x->ch[];
for (; ret->ch[] != null; ret = ret->ch[]);
del(x->ch[], x->v = ret->v);
}
} else {
del(x->ch[d], v);
}
if (x->s) x->push_up();
}
inline void insert(int v) {
insert(root, v);
}
inline void del(int v) {
del(root, v);
}
inline int kth(int k) {
int t;
Node *x = root;
for (; x->s;) {
t = x->ch[]->s;
if (k <= t) x = x->ch[];
else if (t + == k) break;
else k -= t + , x = x->ch[];
}
return x->v;
}
int operator[] (int k) {
return kth(k);
}
}sbt;
int main(){
#ifdef LOCAL
freopen("in.txt", "r", stdin);
freopen("out.txt", "w+", stdout);
#endif
int n, v, c = ;
char buf[];
while (~scanf("%d", &n)) {
sbt.init(); queue<int> q;
printf("Case #%d:\n", c++);
while (n--) {
scanf("%s", buf);
if ('i' == buf[]) {
scanf("%d", &v);
sbt.insert(v), q.push(v);
} else if ('o' == buf[]) {
v = q.front(); q.pop();
sbt.del(v);
} else {
printf("%d\n", sbt[((int)q.size() >> ) + ]);
}
}
}
return ;
}
简单题没啥说的,比较了一下还是红黑树快一些。。
hdu 5249 KPI的更多相关文章
- HDU 5249:KPI(权值线段树)
KPI Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Problem Desc ...
- hdoj 5249 KPI(treap)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5249 思路分析:使用queue记录管道中的值并使用treap能够查询第K大的功能查询第floor(m/ ...
- HDU 5249 离线树状数组求第k大+离散化
KPI Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
- 区间第k大问题 权值线段树 hdu 5249
先说下权值线段树的概念吧 权值平均树 就是指区间维护值为这个区间内点出现次数和的线段树 用这个加权线段树 解决第k大问题就很方便了 int query(int l,int r,int rt,int k ...
- 2015年百度之星初赛(1) --- D KPI
KPI Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
- 大数据慎行,数据管理要落实到KPI
近年来,"大数据"一词被IT和互联网行业广泛提及,但真正落到实处的案例没有多少,大数据量支撑.数据挖掘技术.非结构化数据是阻碍的主要原因.大多数企业的信息化并没有达到到成熟水平,关 ...
- HDOJ 2111. Saving HDU 贪心 结构体排序
Saving HDU Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- KPI:Key Performance Indicator
通信中KPI,是Key Performance Indicators的缩写,意思是关键性能指标.performance 还有绩效:业绩的意思,但显然不适用于这种场合. 通信中KPI的内容有:掉话率.接 ...
- 【HDU 3037】Saving Beans Lucas定理模板
http://acm.hdu.edu.cn/showproblem.php?pid=3037 Lucas定理模板. 现在才写,noip滚粗前兆QAQ #include<cstdio> #i ...
随机推荐
- ctags 文章
http://blog.csdn.net/wuziqi4/article/details/1709722
- 洛谷P1465
P1465 序言页码 Preface Numbering 74通过 111提交 题目提供者该用户不存在 标签USACO 难度普及/提高- 提交 讨论 题解 最新讨论 暂时没有讨论 题目描述 一类书 ...
- noip2008 笨小猴
P1125 笨小猴 1.6K通过 3.7K提交 题目提供者该用户不存在 标签模拟2008NOIp提高组 难度普及- 提交该题 讨论 题解 记录 题目描述 笨小猴的词汇量很小,所以每次做英语选择题的 ...
- TCP/IP详解学习笔记(2)-- 数据链路层
1.概述 数据链路层属于计算机网络的底层,使用的信道主要有点对点信道和广播信道两种类型. 在TCP/IP协议族中,数据链路层主要有三个目的: 1)为IP模块发送 ...
- JS ​ ZERO WIDTH SPACE
如上编码 ZERO WIDTH SPACE 在各个语言的表达. JS中使用 var b = a.replace(/\u200B/g,''); 来替换,去掉 ZERO WIDTH SPACE . ht ...
- npm命令大全
参考网站:https://docs.npmjs.com/ 有了npm,可以很快的找到特定服务要使用的包,进行下载.安装以及管理已经安装的包. npm 常用命令 npm install <name ...
- [python 2.7.5] 实现配置文件的读写
import ConfigParser config = ConfigParser.RawConfigParser() # When adding sections or items, add the ...
- JavaScript for...in 语句
JavaScript for...in 语句 for...in 语句用于遍历数组或者对象的属性(对数组或者对象的属性进行循环操作). for ... in 循环中的代码每执行一次,就会对数组的元素或者 ...
- Volume serial number could associate file existence on certain volume
When it comes to lnk file analysis, we should put more emphasis on the volume serial number. It coul ...
- c89和c99的区别【转】
//本文转自:http://www.360doc.com/content/10/0324/18/2633_20101093.shtml 注: GCC支持C99, 通过 --std=c99 命令行参数开 ...