题目连接

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的更多相关文章

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

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

  2. hdoj 5249 KPI(treap)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5249 思路分析:使用queue记录管道中的值并使用treap能够查询第K大的功能查询第floor(m/ ...

  3. HDU 5249 离线树状数组求第k大+离散化

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

  4. 区间第k大问题 权值线段树 hdu 5249

    先说下权值线段树的概念吧 权值平均树 就是指区间维护值为这个区间内点出现次数和的线段树 用这个加权线段树 解决第k大问题就很方便了 int query(int l,int r,int rt,int k ...

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

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

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

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

  7. HDOJ 2111. Saving HDU 贪心 结构体排序

    Saving HDU Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  8. KPI:Key Performance Indicator

    通信中KPI,是Key Performance Indicators的缩写,意思是关键性能指标.performance 还有绩效:业绩的意思,但显然不适用于这种场合. 通信中KPI的内容有:掉话率.接 ...

  9. 【HDU 3037】Saving Beans Lucas定理模板

    http://acm.hdu.edu.cn/showproblem.php?pid=3037 Lucas定理模板. 现在才写,noip滚粗前兆QAQ #include<cstdio> #i ...

随机推荐

  1. Openstack-Mitaka Ceilometer 中使用 SNMP 监控真实物理机

    Ceilometer 是 Openstack 的监控管理计费模块,我所用的版本为 Mitaka 版本.在 Ceilometer 中,可以使用 SNMP 监控服务器的实时硬件资源信息. 系统环境为 Ce ...

  2. nginx 代理配置文件实例

    安装NGINX前要先安装PCRE正则表达式库: ./configure --prefix=/usr/local/pcre 出现以下错误  (一般./configure即可, 笔者这里是直接./conf ...

  3. 洛谷P2732 商店购物 Shopping Offers

    P2732 商店购物 Shopping Offers 23通过 41提交 题目提供者该用户不存在 标签USACO 难度提高+/省选- 提交  讨论  题解 最新讨论 暂时没有讨论 题目背景 在商店中, ...

  4. 在 JNI 编程中避免内存泄漏

    JAVA 中的内存泄漏 JAVA 编程中的内存泄漏,从泄漏的内存位置角度可以分为两种:JVM 中 Java Heap 的内存泄漏:JVM 内存中 native memory 的内存泄漏. Java H ...

  5. WPF和WINFORM的互操作

    在WPF中使用Winform控件 <Window x:Class="WPFApplication.Window1" xmlns="http://schemas.mi ...

  6. axis2 部署webservice

    1.下载axis2-1.6.4-war.zip 2.将axis2.war防止webapps下. 3.http://localhost:8080/axis2/ 4.myeclipse创建web serv ...

  7. hdu1203

    #include <stdio.h> #include <math.h> #define mmin(x,y) (x)<(y)?(x):(y) int main(){ +] ...

  8. easyui+ashx 动态初始化datagrid(动态列头)

    效果图: 1:简单初始化 JSON格式数据如下(后台自己构建,后台代码略): {"total":6,"columns":[{"field": ...

  9. MallBuilder 多用户商城管理系统 v5.8.1.1

    MallBuilder是一款基于PHP+MYSQL的多用户网上商城解决方案.利用MallBuilder可以快速建立一个功能强大的类似京东商城.天猫商城.1号店商城的网上商城,或企业.行业化.本地化和垂 ...

  10. Template_Method

    #include <iostream> using namespace std; #define DESTROY_POINTER(ptr) if (ptr) { delete ptr; p ...