hdu 3282 Running Median
题目连接
http://acm.hdu.edu.cn/showproblem.php?pid=3282
Running Median
Description
For this problem, you will write a program that reads in a sequence of 32-bit signed integers. After each odd-indexed value is read, output the median (middle value) of the elements received so far.
Input
The first line of input contains a single integer $P, (1 \leq P \leq 1000)$, which is the number of data sets that follow. The first line of each data set contains the data set number, followed by a space, followed by an odd decimal integer $M, (1 \leq M \leq 9999)$, giving the total number of signed integers to be processed.
The remaining line(s) in the dataset consists of the values, 10 per line, separated by a single space.
The last line in the dataset may contain less than 10 values.
Output
For each data set the first line of output contains the data set number, a single space and the number of medians output (which should be one-half the number of input values plus one). The output medians will be on the following lines, 10 per line separated by a single space. The last line may have less than 10 elements, but at least 1 element. There should be no blank lines in the output.
SampleInput
3
1 9
1 2 3 4 5 6 7 8 9
2 9
9 8 7 6 5 4 3 2 1
3 23
23 41 13 22 -3 24 -31 -11 -8 -7
3 5 103 211 -311 -45 -67 -73 -81 -99
-33 24 56
SampleOutput
1 5
1 2 3 4 5
2 5
9 8 7 6 5
3 12
23 23 22 22 13 3 5 5 3 -3
-7 -3
动态的求中位数,套个平衡树即可。。
#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<vector>
#include<map>
#include<set>
using std::cin;
using std::cout;
using std::endl;
using std::find;
using std::sort;
using std::set;
using std::map;
using std::pair;
using std::vector;
#define sz(c) (int)(c).size()
#define all(c) (c).begin(), (c).end()
#define iter(c) decltype((c).begin())
#define cls(arr,val) memset(arr,val,sizeof(arr))
#define cpresent(c, e) (find(all(c), (e)) != (c).end())
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define fork(i, k, n) for (int i = (int)k; i <= (int)n; i++)
#define tr(c, i) for (iter(c) i = (c).begin(); i != (c).end(); ++i)
#define pb(e) push_back(e)
#define mp(a, b) make_pair(a, b)
const int Max_N = ;
typedef unsigned long long ull;
struct Node {
int v, s;
Node *ch[];
inline void setc(int _v, int _s, Node *p) {
v = _v, s = _s;
ch[] = ch[] = p;
}
inline void push_up() {
s = ch[]->s + ch[]->s + ;
}
};
struct SBT {
Node stack[Max_N];
Node *root, *null, *tail;
inline void init() {
tail = &stack[];
null = tail++;
null->setc(, , NULL);
root = null;
}
inline Node *newNode(int v) {
Node *p = tail++;
p->setc(v, , null);
return p;
}
inline void rotate(Node *&x, bool 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, bool d) {
if (!x->ch[d]->s) 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->s) { x = newNode(v); return; }
bool d = v > x->v; x->s++;
insert(x->ch[d], v);
x->push_up();
Maintain(x, d);
}
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->v;
}
inline void go() {
vector<int> res;
int v, q, n, k = ;
scanf("%d %d", &q, &n);
printf("%d %d\n", q, (n + ) >> );
fork(i, , n) {
scanf("%d", &v);
insert(root, v);
if (i & ) res.push_back(kth((root->s >> ) + ));
}
n = sz(res);
rep(i, n) {
if ((i + ) % ) {
if (i == n - ) printf("%d\n", res[i]);
else printf("%d ", res[i]);
}
else printf("%d\n", res[i]);
}
}
}sbt;
int main() {
#ifdef LOCAL
freopen("in.txt", "r", stdin);
freopen("out.txt", "w+", stdout);
#endif
int t;
scanf("%d", &t);
while (t--){
sbt.init();
sbt.go();
}
return ;
}
hdu 3282 Running Median的更多相关文章
- 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
2015-07-16 问题简述: 动态求取中位数的问题,输入一串数字,每输入第奇数个数时求取这些数的中位数. 原题链接:http://poj.org/problem?id=3784 解题思路: 求取中 ...
- 【POJ 3784】 Running Median (对顶堆)
Running Median Description For this problem, you will write a program that reads in a sequence of 32 ...
- 【POJ3784】Running Median
Running Median Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 3406 Accepted: 1576 De ...
- POJ3784 Running Median
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 1670 Accepted: 823 Description For th ...
- Running Median POJ - 3784 (对顶堆/优先队列 | 链表)
For this problem, you will write a program that reads in a sequence of 32-bit signed integers. After ...
- hdu 4452 Running Rabbits 模拟
Running RabbitsTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- POJ 3784 Running Median(动态维护中位数)
Description For this problem, you will write a program that reads in a sequence of 32-bit signed int ...
- POJ 3784 Running Median【维护动态中位数】
Description For this problem, you will write a program that reads in a sequence of 32-bit signed int ...
随机推荐
- kubernetes学习笔记1
安装篇 Downloading Kubernetes You can either build a release from sources or download a pre-built relea ...
- jQuery中 wrap() wrapAll() 与 wrapInner()的区别
今晚看书的时候发现jQuery有三个包裹节点的方法,百度了一下jQuery wrap() / wrapAll() / wrapInner(),果然搜索结果 W3School的文档说明是排第一的. 可是 ...
- JS判断手机浏览器
<script type="text/javascript"> /* * 智能机浏览器版本信息: * */ varbrowser={ versions:function ...
- DWR基本配置
DWR——Direct Web Remoter Servlet 供给那些想要以一种简单的方式使用Ajax和XMLHttpRequest的开发者.它具有一套JavaScript功能集,它们把从HTML页 ...
- css兼容
1.不同浏览器默认边距不同,必须对body自定义:margin:0;padding:0; 2.margin.padding属性值为%时,不是所有浏览器都支持: 3.transparent属性,IE7之 ...
- Android IOS WebRTC 音视频开发总结(十七)-- 调试技巧
本文章主要介绍WEBRTC在各平台下调试或日志查看方式,以方便问题排查,包括BS,PC,Android,IOS(本系列文章转载请说明出处,博客园RTC.Blacker). 1,浏览器开发: 这种开发方 ...
- CentOS6.5安装readline时报错:/usr/bin/ld : cannot find -lncurses
CentOS6.5安装readline时报错:/usr/bin/ld : cannot find -lncurses 解决方法: 安装ncurses-devel,输入命令: #yum install ...
- iOS中僵尸对象的实现方法
什么是僵尸对象?所谓僵尸,就是过度释放的对象.在ios开发中,僵尸对象对于开发人员调试程序来说很有用.我们通常将NSZombieEnabled环境变量设置为YES来打开僵尸对象,但这会导致所有的对象都 ...
- Windows Phone 显示长文本
文采不好,将就着看,见谅 思路最重要,故本文不提供源码下载 参考项目: http://www.windowsphone.com/zh-cn/store/app/%E5%BF%83%E7%90%86fm ...
- 三个有用的SQL辅助工具
三个有用的SQL辅助工具 编写人:CC阿爸 2015-1-23 工欲善其事必先利其器,今天在这里,我想与大家一起分享三个有用的SQL辅助工具.有兴趣的同学,可以一同探讨与学习一下,否则就略过吧. 相信 ...