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

动态的求中位数,套个平衡树即可。。

  1 #include<algorithm>
  2 #include<iostream>
  3 #include<cstdlib>
  4 #include<cstring>
  5 #include<cstdio>
  6 #include<vector>
  7 #include<map>
  8 #include<set>
  9 using std::cin;
 10 using std::cout;
 11 using std::endl;
 12 using std::find;
 13 using std::sort;
 14 using std::set;
 15 using std::map;
 16 using std::pair;
 17 using std::vector;
 18 #define sz(c) (int)(c).size()
 19 #define all(c) (c).begin(), (c).end()
 20 #define iter(c) decltype((c).begin())
 21 #define cls(arr,val) memset(arr,val,sizeof(arr))
 22 #define cpresent(c, e) (find(all(c), (e)) != (c).end())
 23 #define rep(i, n) for (int i = 0; i < (int)(n); i++)
 24 #define fork(i, k, n) for (int i = (int)k; i <= (int)n; i++)
 25 #define tr(c, i) for (iter(c) i = (c).begin(); i != (c).end(); ++i)
 26 #define pb(e) push_back(e)
 27 #define mp(a, b) make_pair(a, b)
 28 const int Max_N = 10010;
 29 typedef unsigned long long ull;
 30 struct Node {
 31     int v, s;
 32     Node *ch[2];
 33     inline void setc(int _v, int _s, Node *p) {
 34         v = _v, s = _s;
 35         ch[0] = ch[1] = p;
 36     }
 37     inline void push_up() {
 38         s = ch[0]->s + ch[1]->s + 1;
 39     }
 40 };
 41 struct SBT {
 42     Node stack[Max_N];
 43     Node *root, *null, *tail;
 44     inline void init() {
 45         tail = &stack[0];
 46         null = tail++;
 47         null->setc(0, 0, NULL);
 48         root = null;
 49     }
 50     inline Node *newNode(int v) {
 51         Node *p = tail++;
 52         p->setc(v, 1, null);
 53         return p;
 54     }
 55     inline void rotate(Node *&x, bool d) {
 56         Node *k = x->ch[!d]; x->ch[!d] = k->ch[d]; k->ch[d] = x;
 57         k->s = x->s;
 58         x->push_up();
 59         x = k;
 60     }
 61     inline void Maintain(Node *&x, bool d) {
 62         if (!x->ch[d]->s) return;
 63         if (x->ch[d]->ch[d]->s > x->ch[!d]->s) rotate(x, !d);
 64         else if (x->ch[d]->ch[!d]->s > x->ch[!d]->s) rotate(x->ch[d], d), rotate(x, !d);
 65         else return;
 66         Maintain(x, 0), Maintain(x, 1);
 67     }
 68     inline void insert(Node *&x, int v) {
 69         if (!x->s) { x = newNode(v); return; }
 70         bool d = v > x->v; x->s++;
 71         insert(x->ch[d], v);
 72         x->push_up();
 73         Maintain(x, d);
 74     }
 75     inline int kth(int k) {
 76         int t;
 77         Node *x = root;
 78         for (; x->s;) {
 79             t = x->ch[0]->s;
 80             if (k == t + 1) break;
 81             else if (k <= t) x = x->ch[0];
 82             else k -= t + 1, x = x->ch[1];
 83         }
 84         return x->v;
 85     }
 86     inline void go() {
 87         vector<int> res;
 88         int v, q, n, k = 1;
 89         scanf("%d %d", &q, &n);
 90         printf("%d %d\n", q, (n + 1) >> 1);
 91         fork(i, 1, n) {
 92             scanf("%d", &v);
 93             insert(root, v);
 94             if (i & 1) res.push_back(kth((root->s >> 1) + 1));
 95         }
 96         n = sz(res);
 97         rep(i, n) {
 98             if ((i + 1) % 10) {
 99                 if (i == n - 1) printf("%d\n", res[i]);
100                 else printf("%d ", res[i]);
101             }
102             else  printf("%d\n", res[i]);
103         }
104     }
105 }sbt;
106 int main() {
107 #ifdef LOCAL
108     freopen("in.txt", "r", stdin);
109     freopen("out.txt", "w+", stdout);
110 #endif
111     int t;
112     scanf("%d", &t);
113     while (t--){
114         sbt.init();
115         sbt.go();
116     }
117     return 0;
118 }

时间: 2024-07-31 22:07:53

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?pid=3282 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

【POJ3784】Running Median

Running Median Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3406   Accepted: 1576 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

POJ 3784 Running Median

Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 1670   Accepted: 823 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 (midd

hdu 4452 Running Rabbits 模拟

Running RabbitsTime Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1344    Accepted Submission(s): 925 Problem DescriptionRabbit Tom and rabbit Jerry are running in a field. The field is an N×N grid

POJ 3784 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

Running Median POJ - 3784 (对顶堆/优先队列)

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 int

POJ 3784 Running Median (最大最小堆)

最大最小堆动态求中位数 题意:输入M个数,当已输入的个数为奇数个时输出此时的中位数. 一共有M/2+1个中位数要输出,每一行10个. 分析: 用两个优先队列来模拟最大最小堆.中位数是x,就是有一半数比x小,一半数比x大. 刚好符合堆的特点. 用一个从大到小排序的优先队列q1来模拟小于x的数. 从小到大排序的优先队列q2来模拟大于x的数. 动态维护两个优先队列的元素个数.q1.size()=q2.size() 输入的数为偶数个时, q1.size()=q2.size()+1 输入的数为奇数个时.

[模拟] hdu 4452 Running Rabbits

题意: 两个人一个人在(1,1),一个人在(N,N) 给每个人每秒移动的速度v,和一个s代表移动s秒后左转方向 特别注意的是如果撞墙,要反弹回去,方向改变 比如在(1,1),往左走一步到(1,0) 其实就是走到了(1,2) 然后如果两个人见面那么交换方向并且不再左转! 思路: 直接模拟.. 代码: #include"cstdlib" #include"cstdio" #include"cstring" #include"cmath&qu

POJ 3784 Running Median 动态求中位数 堆

题意. 1000个case 每个case 输入若干个数,对第k个输入,如果k为奇数,则输出前k个数的中位数 那么这就是动态求中位数了 实现的思路也比较简洁 用两个堆, 大顶堆和小顶堆 每次输入一个数,如果这个数比当前的中位数大,就存入小顶堆中,  否则就存入大顶堆. 然后调整, 小顶堆元素的个数要等于大顶堆的元素个数,或者比其多1. 如果小顶堆的元素太多,就塞到大顶堆里,反之亦然 这样一来就会发现.小顶堆的元素比所有大顶堆的元素都大, 而且小顶堆的堆顶就是中位数. 那么怎么样才能想到这样一个思路