[hdu5249]动态中位数
题意:3种操作分别为入队,出队,查询当前队列的中位数。操作数为1e5数量级。
思路:先考虑离线算法,可以离散+线段树,可以划分树,考虑在线算法,则有treap名次树,SBtree(size balanced tree)等等。
///这个模板有问题,别再用了。。。!!!!
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
|
#include <iostream>#include <cstdio>#include <cstring>#include <cstdlib>#include <queue>using namespace std;const int maxn = 1e5 + 7;template<typename T> class SBTree {public: SBTree() { root = nil = new Node(); nil->ch[0] = nil->ch[1] = nil; nil->sz = 0; tot = top = 0; } ~SBTree() {} void clear() { root = nil; tot = top = 0; } int size() { return root->sz; } bool empty() { return root == nil; } void insert(const T &it) { insert(root, it); } void erase(const T &it) { erase(root, it); } bool find(const T &it) { return find(root, it); } const T &minItem() { return minMax(root, 0); } const T &maxItem() { return minMax(root, 1); } const T &select(int k) { return select(root, k); } int rank(const T &it) { return rank(root, it); }private: const static int maxn = 1e4 + 7; struct Node { T key; int sz; Node *ch[2]; } v[maxn], *stk[maxn], *root, *nil; int tot, top; void rotate(Node *&x, int d) { Node *y = x->ch[d ^ 1]; x->ch[d ^ 1] = y->ch[d]; y->ch[d] = x; y->sz = x->sz; x->sz = x->ch[0]->sz + x->ch[1]->sz + 1; x = y; } void maintain(Node *&x, int d) { if (x == nil) return; if (x->ch[d]->sz < x->ch[d ^ 1]->ch[d ^ 1]->sz) rotate(x, d); else if (x->ch[d]->sz < x->ch[d ^ 1]->ch[d]->sz) { rotate(x->ch[d ^ 1], d ^ 1); rotate(x, d); } else { return; } maintain(x->ch[0], 1); maintain(x->ch[1], 0); maintain(x, 1); maintain(x, 0); } void insert(Node *&x, const T &it) { if (x == nil) { x = top ? stk[top--] : v + tot++; x->key = it; x->sz = 1; x->ch[0] = x->ch[1] = nil; } else { ++x->sz; insert(x->ch[x->key < it], it); maintain(x, it < x->key); } } void erase(Node *&x, const T &it) { Node *p; if (x == nil) return; --x->sz; if (it < x->key) erase(x->ch[0], it); else if (x->key < it) erase(x->ch[1], it); else { if (x->ch[1] == nil) { stk[++top] = x; x = x->ch[0]; } else { for (p = x->ch[1]; p->ch[0] != nil; p = p->ch[0]); erase(x->ch[1], x->key = p->key); } } } bool find(const Node *x, const T &it) { if (x == nil || !(it < x->key || x->key < it)) return x != nil; return find(x->ch[x->key < it], it); } const T &minMax(const Node *x, int d) { return x->ch[d] == nil ? x->key : minMax(x->ch[d], d); } const T &select(const Node *x, int k) { if (x == nil || k == x->ch[0]->sz + 1) return x->key; return select(x->ch[x->ch[0]->sz + 1 < k], x->ch[0]->sz + 1 < k ? k - x->ch[0]->sz - 1 : k); } int rank(const Node *x, const T &it) { if (x == nil) return 1; if (it < x->key) return rank(x->ch[0], it); if (x->key < it) return rank(x->ch[1], it) + x->ch[0]->sz + 1; return x->ch[0]->sz + 1; }};SBTree<int> sbt;int main() {#ifndef ONLINE_JUDGE freopen("in.txt", "r", stdin);#endif // ONLINE_JUDGE int cas = 0, n; while (cin >> n) { printf("Case #%d:\n", ++ cas); sbt.clear(); queue<int> Q; while (n --) { char s[10]; scanf("%s", s); if (s[0] == 'i') { int x; scanf("%d", &x); sbt.insert(x); Q.push(x); } else if (s[0] == 'o') { int x = Q.front(); Q.pop(); sbt.erase(x); } else { int sz = Q.size(); printf("%d\n", sbt.select(sz / 2 + 1)); } } } return 0;} |
[hdu5249]动态中位数的更多相关文章
- AcWing:106. 动态中位数(对顶堆)
依次读入一个整数序列,每当已经读入的整数个数为奇数时,输出已读入的整数构成的序列的中位数. 输入格式 第一行输入一个整数PP,代表后面数据集的个数,接下来若干行输入各个数据集. 每个数据集的第一行首先 ...
- HDU 3282 Running Median 动态中位数,可惜数据范围太小
Running Median Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pi ...
- POJ 3784 Running Median (动态中位数)
题目链接:http://poj.org/problem?id=3784 题目大意:依次输入n个数,每当输入奇数个数的时候,求出当前序列的中位数(排好序的中位数). 此题可用各种方法求解. 排序二叉树方 ...
- hdu5249KPI动态中位数(两个set)
题意(中问题直接粘题意吧) KPI Problem Descr ...
- 动态中位数-POJ 3784
题目: 依次读入一个整数序列,每当已经读入的整数个数为奇数时,输出已读入的整数构成的序列的中位数. 输入格式 第一行输入一个整数P,代表后面数据集的个数,接下来若干行输入各个数据集. 每个数据集的第一 ...
- POJ 3784 Running Median【维护动态中位数】
Description For this problem, you will write a program that reads in a sequence of 32-bit signed int ...
- AcWing 106. 动态中位数
依次读入一个整数序列,每当已经读入的整数个数为奇数时,输出已读入的整数构成的序列的中位数. #include<bits/stdc++.h> using namespace std; pri ...
- luogu_1168: 中位数
洛谷1168:中位数(对顶堆) 题目描述: 给定一个长度为\(N\)的非负整数序列\(A_i\),对于所有\((1\leq k\leq\frac{N+1}{2})\),输出\(A_1,A_3,..., ...
- 牛客练习赛63 C 牛牛的揠苗助长 主席树 二分 中位数
LINK:牛牛的揠苗助长 题目很水 不过做法很多 想到一个近乎O(n)的做法 不过感觉假了 最后决定莽一个主席树 当然 平衡树也行. 容易想到 答案为ans天 那么一些点的有效增长项数为 ans%n. ...
随机推荐
- 关于json转义中文
服务器传递或者程序传递中,不识别读取到的JSON数据中 \u开头的数据. PHP 生成JSON的时候,必须将汉字不转义为 \u开头的UNICODE数据. 网上很多,但是其实都是错误的,正确的方法是在j ...
- Ansible playbook Vault 加密
Ansible playbook Vault 加密详解与使用案例 主机规划 添加用户账号 说明: 1. 运维人员使用的登录账号: 2. 所有的业务都放在 /app/ 下「yun用户的家目录」,避免业务 ...
- Linux - centos7.X安装tomcat8
创建tomcat安装路径 mkdir /usr/local/tomcat wget直接下载tomcat8 注意,需要已经安装了wget命令 wget http://mirrors.estointern ...
- VC++ QT 数组的初始化
数组有时会初始化为0. 但加了一个 QThread 的派生类对象之后,数组就不再被初始化为0了. 所以对于数组还是要手动初始化,否则可能产生无法预料的现象.
- 日志分析工具ELK(五)
八.Kibana实践 选择绝对时间和相对时间 搜索 还可以添加相关信息 自动刷新页面时间,也可以关闭 创建图像,可视化 编辑Markdown,创建一个值班联系表 值班联系表 保存 再创建一个饼图;查看 ...
- Spring Boot的TestRestTemplate使用
文章目录 添加maven依赖 TestRestTemplate VS RestTemplate 使用Basic Auth Credentials 使用HttpClientOption 使用RestTe ...
- 使用3种协议搭建yum仓库
制作本地yum仓库 开启服务一般要关闭防火墙,selinux之后再reboot ## 方案一:FTP协议------ftp://IP 下载vsftpd---启动vsftpd---ftp://10.0. ...
- 获取系统DPI、系统显示比例等
using System; using System.Drawing; using System.Runtime.InteropServices; namespace XYDES { public c ...
- log4j MDC NDC详解
NDC ( Nested Diagnostic Context )和 MDC ( Mapped Diagnostic Context )是 log4j 种非常有用的两个类,它们用于存储应用程序的上下文 ...
- C++课程设计,12306模拟写起来就是这么粗暴
这篇文章很详细,也很多希望可以好好看看!看完C++稳过! 一.12306应该具备那些功能 1.查询(一个月以内的): 1.查车票:出发地+目的地+出发时间->显示经过两站车票信息 (余票,车次信 ...