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 ...
随机推荐
- ORA-00933: SQL command not properly ended
今天写了一个小的SQL语句类似下面的这句: UPDATE A SET ID=B.ID FROM A,B WHERE A.NAME=B.NAME 在执行时居然报了“ORA-00933: SQL comm ...
- 匿名管道 远程cmd
管道是单向的,传送数据的方向是固定的,所以互相通信需要两个管道. STARTUPINFO si; ZeroMemory(&si,sizeof(si)); si.dwFlags = STARTF ...
- List<string>里 每个元素重复了多少次
List<string>里 每个元素重复了多少次 static void Main(string[] args) { List<string> list = new List& ...
- PAT1038. Recover the Smallest Number
//意识到一个重要错误,一直以为atoi,itoa是windows独有的,linux下不可用,直到刚刚... //string+=比strcat好用多了,字符比较也方便的多,但是用scanf读入str ...
- .NET 命名规范 代码示例
class Person { /// <summary> /// 公有字段.属性 首字母大写 /// </summary> public string FirstName; p ...
- Nginx 下无法读取session 导致 thinkphp验证码错误
打开php配置文件 php.ini 使用搜索命令 whereis php.ini 一般在:/etc/php.ini 目录下 使用vim命令打开 找到: session.save_path 找到php保 ...
- 使用OrderBy对List<Person>集合排序
string sortOrder = Request.QueryString["sortOrder"]; string sortField = Request.QueryStr ...
- Git客户端图文详解如何安装配置GitHub操作流程攻略
收藏自 http://www.ihref.com/read-16377.html Git介绍 分布式 : Git版本控制系统是一个分布式的系统, 是用来保存工程源代码历史状态的命令行工具; 保存点 : ...
- php 获取文件后缀名
$file_ext = strtolower(substr(strrchr($upload_file, '.'), 1)); strrchr:查找指定字符在字符串中的最后一次出现 string str ...
- VB 进制转换大全
'二进制转十进制 Public Function B2D(vBStr As String) As Long Dim vLen As Integer '串长 Dim vDec As Long '结果 D ...