zoukankan      html  css  js  c++  java
  • Hdu 4251 区间中位数(划分树)

    题目链接

    The Famous ICPC Team Again

    Time Limit: 30000/15000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 796    Accepted Submission(s): 388


    Problem Description
    When Mr. B, Mr. G and Mr. M were preparing for the 2012 ACM-ICPC World Final Contest, Mr. B had collected a large set of contest problems for their daily training. When they decided to take training, Mr. B would choose one of them from the problem set. All the problems in the problem set had been sorted by their time of publish. Each time Prof. S, their coach, would tell them to choose one problem published within a particular time interval. That is to say, if problems had been sorted in a line, each time they would choose one of them from a specified segment of the line.

    Moreover, when collecting the problems, Mr. B had also known an estimation of each problem’s difficultness. When he was asked to choose a problem, if he chose the easiest one, Mr. G would complain that “Hey, what a trivial problem!”; if he chose the hardest one, Mr. M would grumble that it took too much time to finish it. To address this dilemma, Mr. B decided to take the one with the medium difficulty. Therefore, he needed a way to know the median number in the given interval of the sequence.
    Input
    For each test case, the first line contains a single integer n (1 <= n <= 100,000) indicating the total number of problems. The second line contains n integers xi (0 <= xi <= 1,000,000,000), separated by single space, denoting the difficultness of each problem, already sorted by publish time. The next line contains a single integer m (1 <= m <= 100,000), specifying number of queries. Then m lines follow, each line contains a pair of integers, A and B (1 <= A <= B <= n), denoting that Mr. B needed to choose a problem between positions A and B (inclusively, positions are counted from 1). It is guaranteed that the number of items between A and B is odd.
    Output
    For each query, output a single line containing an integer that denotes the difficultness of the problem that Mr. B should choose.
    Sample Input
    5 5 3 2 4 1 3 1 3 2 4 3 5 5 10 6 4 8 2 3 1 3 2 4 3 5
    Sample Output
    Case 1: 3 3 2 Case 2: 6 6 4
    今天的最后一题,我现在在写解题报告,,,有种要猝死的感觉,特么今天敲代码敲的精疲力尽。
    A了5题,全是数据结果,什么线段树,归并树, 划分树,并查集,快速选择。。。
    对了,这题就是用划分树做的。。。1A, 题意是求区间中位数,转化为求区间第k大,即(R - L) / 2;
    Accepted Code:
     1 /*************************************************************************
     2     > File Name: 4251.cpp
     3     > Author: Stomach_ache
     4     > Mail: sudaweitong@gmail.com
     5     > Created Time: 2014年08月02日 星期六 23时02分48秒
     6     > Propose: 
     7  ************************************************************************/
     8 
     9 #include <cmath>
    10 #include <string>
    11 #include <cstdio>
    12 #include <fstream>
    13 #include <cstring>
    14 #include <iostream>
    15 #include <algorithm>
    16 using namespace std;
    17 
    18 const int maxn = 100002;
    19 int n, m;
    20 int a[maxn], nums[maxn];
    21 int toLeft[20][maxn];
    22 int tr[20][maxn];
    23 
    24 void build(int d, int l, int r) {
    25     int mid = (l + r) / 2;
    26     int le = 0, ls = l, rs = mid + 1;
    27     for (int i = mid; i >= l; i--) { 
    28           if (nums[i] == nums[mid]) le++;
    29         else break;
    30     }
    31     for (int i = l; i <= r; i++) {
    32           if (i == l) toLeft[d][i] = 0;
    33         else toLeft[d][i] = toLeft[d][i-1];
    34         
    35         if (tr[d][i] < nums[mid]) {
    36               toLeft[d][i]++;
    37             tr[d+1][ls++] = tr[d][i];
    38         } else if (tr[d][i] > nums[mid]) {
    39               tr[d+1][rs++] = tr[d][i];
    40         } else {
    41               if (le) {
    42                   le--;
    43                 toLeft[d][i]++;
    44                 tr[d+1][ls++] = tr[d][i];
    45             } else {
    46                   tr[d+1][rs++] = tr[d][i];
    47             }
    48         }
    49     }
    50     if (l == r) return ;
    51     build(d+1, l, mid);
    52     build(d+1, mid+1, r);
    53 }
    54 
    55 int query(int d, int l, int r, int ql, int qr, int k) {
    56       if (l == r) return tr[d][l];
    57     int s = (ql == l ? 0 : toLeft[d][ql-1]);
    58     int ss = toLeft[d][qr];
    59     int mid = (l + r) / 2;
    60     if (ss - s >= k) return query(d+1, l, mid, l+s, l+ss-1, k);
    61     else return query(d+1, mid+1, r, mid+1+(ql-l-s), mid+1+(qr-l-ss), k-(ss-s));
    62 }
    63 
    64 int main(void) {
    65       int cas = 1;
    66       while(~scanf("%d", &n)) {
    67           memset(toLeft, 0, sizeof(toLeft));
    68         memset(tr, 0, sizeof(tr));
    69           for (int i = 1; i <= n; i++) {
    70               scanf("%d", a + i);
    71             nums[i] = a[i];
    72             tr[0][i] = a[i];
    73         }
    74         sort(nums + 1, nums + n + 1);
    75         build(0, 1, n);
    76         printf("Case %d:
    ", cas++);
    77         scanf("%d", &m);
    78         while (m--) {
    79               int l, r;
    80             scanf("%d %d", &l, &r);
    81             int ans = query(0, 1, n, l, r, (r-l)/2+1);
    82             printf("%d
    ", ans);
    83         }
    84     }
    85 
    86     return 0;
    87 }
     
  • 相关阅读:
    Python的条件判断和循环
    Python分支和循环结构的练习
    Python的变量和运算符
    Python简介
    Git的使用及网络编程多线程多进程
    函数面向对象编程及文件的读取
    函数模块字符串列表
    python变量运算符分支结构循环结构及例题
    集合
    三级菜单
  • 原文地址:https://www.cnblogs.com/Stomach-ache/p/3887733.html
Copyright © 2011-2022 走看看