zoukankan      html  css  js  c++  java
  • 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
    ", 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
    ", res[i]);
    100                 else printf("%d ", res[i]);
    101             }
    102             else  printf("%d
    ", 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 }
    View Code
    By: GadyPu 博客地址:http://www.cnblogs.com/GadyPu/ 转载请说明
  • 相关阅读:
    深入Vue.js从源码开始(二)
    Vue.js的动态组件模板
    Vue中的methods、watch、computed
    Understand .sync in Vue
    vue程序中组件间的传值方式
    xUtils框架的介绍(一)
    xUtils框架的介绍(二)
    Java如何计算hashcode值
    Java网络编程总结
    深入Java线程管理(五):线程池
  • 原文地址:https://www.cnblogs.com/GadyPu/p/4581471.html
Copyright © 2011-2022 走看看