题目连接

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

  1. HDU 3282 Running Median 动态中位数,可惜数据范围太小

    Running Median Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pi ...

  2. POJ 3784.Running Median

    2015-07-16 问题简述: 动态求取中位数的问题,输入一串数字,每输入第奇数个数时求取这些数的中位数. 原题链接:http://poj.org/problem?id=3784 解题思路: 求取中 ...

  3. 【POJ 3784】 Running Median (对顶堆)

    Running Median Description For this problem, you will write a program that reads in a sequence of 32 ...

  4. 【POJ3784】Running Median

    Running Median Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3406   Accepted: 1576 De ...

  5. POJ3784 Running Median

    Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 1670   Accepted: 823 Description For th ...

  6. Running Median POJ - 3784 (对顶堆/优先队列 | 链表)

    For this problem, you will write a program that reads in a sequence of 32-bit signed integers. After ...

  7. hdu 4452 Running Rabbits 模拟

    Running RabbitsTime Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  8. POJ 3784 Running Median(动态维护中位数)

    Description For this problem, you will write a program that reads in a sequence of 32-bit signed int ...

  9. POJ 3784 Running Median【维护动态中位数】

    Description For this problem, you will write a program that reads in a sequence of 32-bit signed int ...

随机推荐

  1. NFS服务器

    NFS是网络文件系统(Network File System)的简称,可实现在多种网络上共享和装配远程文件系统.最大的功能是通过网络,让不同操作系统的计算机共享数据,所以它可以看成一个文件服务器. 1 ...

  2. php 5.6.14手动安装 php -v 显示没有安装

    奇怪了,今天利用源码手动php,安装成功后,利用php -v提示没有安装,which php也是有问题,php文件也没有办法执行 搞了半天,发现是没有添加环境变量,╮(╯▽╰)╭ 方法: 修改/etc ...

  3. jquery的end(),addBack()方法example

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  4. win7和win8如何设置快速启动栏

    a.在任务栏上右键 -> 工具栏 -> 新建工具栏 -> 跳出选择文件夹对话框,在文件夹里面(光标山洞处)输入这个路径,然后按回车: %userprofile%\AppData\Ro ...

  5. jQuery插件之Cookie

    一.jQuery.Cookie.js插件是一个轻量级的Cookie管理插件. 特别提醒,今日发现一个特别的错误,google浏览器提示:has no method $.cookie.火狐浏览器提示:$ ...

  6. WeChat 6.3 wipe deleted chat messages as well as LINE 5.3 and above

    Let me show you the WeChat version first. It is 6.3. What will happen to WeChat deleted chat message ...

  7. 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 ...

  8. Sublime Text 2/3安装CTags实现函数跳转

    安装ctags 下载 ctags程序,放到目录D:/ctags/下 安装ctags插件 1. 打开Sublime Text 2. Preferences->Package Control-> ...

  9. Android IOS WebRTC 音视频开发总结(十四)-- sip和xmpp异同

    这篇文章主要介绍XMPP与SIP,很多人容易混淆这两个概念,转载请说明出处(博客园RTC.Blacker). 简介:XMPP和SIP都是应用层协议,主要用于互联网上发送语音和即时通讯. SIP在RFC ...

  10. Android IOS WebRTC 音视频开发总结(七)-- 基于浏览器的开发

    前面写的一系列总结都是讲webrtc如何下载,编译,开发的,有些人可能有点云里雾里了,WEBRTC不是用来搞跨浏览器开发的吗,怎么我讲的这些跟浏览器扯不上任何关系,其实看看下面这个架构图,你就明白了, ...