zoukankan      html  css  js  c++  java
  • (平方分割)POJ 2104 K-th Number

    You are working for Macrohard company in data structures department. After failing your previous task about key insertion you were asked to write a new data structure that would be able to return quickly k-th order statistics in the array segment. 
    That is, given an array a[1...n] of different integer numbers, your program must answer a series of questions Q(i, j, k) in the form: "What would be the k-th number in a[i...j] segment, if this segment was sorted?" 
    For example, consider the array a = (1, 5, 2, 6, 3, 7, 4). Let the question be Q(2, 5, 3). The segment a[2...5] is (5, 2, 6, 3). If we sort this segment, we get (2, 3, 5, 6), the third number is 5, and therefore the answer to the question is 5.

    Input

    The first line of the input file contains n --- the size of the array, and m --- the number of questions to answer (1 <= n <= 100 000, 1 <= m <= 5 000). 
    The second line contains n different integer numbers not exceeding 109 by their absolute values --- the array for which the answers should be given. 
    The following m lines contain question descriptions, each description consists of three numbers: i, j, and k (1 <= i <= j <= n, 1 <= k <= j - i + 1) and represents the question Q(i, j, k).

    Output

    For each question output the answer to it --- the k-th number in sorted a[i...j] segment.

    Sample Input

    7 3
    1 5 2 6 3 7 4
    2 5 3
    4 4 1
    1 7 3

    Sample Output

    5
    6
    3

    Hint

    This problem has huge input,so please use c-style input(scanf,printf),or you may got time limit exceed.

    采用《挑战程序设计竞赛》上介绍的做法,将整个数组划分为若干个桶(这里桶的大小选为1024,之前1000的时候会TLE)。二分可能的答案。注意到第区间内第k个的必要条件为小于等于该数的数的个数大于等于k,以此作为二分方向的判定条件即可。

     1 #include <iostream>
     2 #include <string>
     3 #include <algorithm>
     4 #include <cstring>
     5 #include <cstdio>
     6 #include <cmath>
     7 #include <queue>
     8 #include <set>
     9 #include <map>
    10 #include <list>
    11 #include <vector>
    12 #include <stack>
    13 #define mp make_pair
    14 #define MIN(a,b) (a>b?b:a)
    15 #define rank rankk
    16 //#define MAX(a,b) (a>b?a:b)
    17 typedef long long ll;
    18 typedef unsigned long long ull;
    19 const int MAX=1e5+5;
    20 const int INF=1e9+5;
    21 const int B=1024;//桶的大小
    22 const double M=4e18;
    23 using namespace std;
    24 const int MOD=1e9+7;
    25 typedef pair<int,int> pii;
    26 const double eps=0.000000001;
    27 int n,m,an,l,r,k,teml,temr;
    28 int a[MAX],num[MAX];
    29 vector <int> bucket[1005];
    30 int main()
    31 {
    32     scanf("%d%d",&n,&m);
    33     for(int i=0;i<n;i++)
    34         scanf("%d",&a[i]);
    35     for(int i=0;i<n;i++)
    36     {
    37         bucket[i/B].push_back(a[i]);
    38         num[i]=a[i];
    39     }
    40     sort(num,num+n);
    41     for(int i=0;i<n/B;i++)
    42         sort(bucket[i].begin(),bucket[i].end());
    43     while(m--)
    44     {
    45         scanf("%d%d%d",&teml,&temr,&k);
    46         int zuo,you,mid,cnt;
    47         zuo=-1,you=n-1;
    48         --teml;
    49         while(you-zuo>1)
    50         {
    51             mid=(zuo+you)/2;
    52             cnt=0;
    53             l=teml;r=temr;
    54             int now=num[mid];//找区间中比二分当下值小于等于的个数,该数应大于等于k
    55             while(l<r&&l%B!=0)
    56                 if(a[l++]<=now)
    57                     ++cnt;
    58             while(l<r&&r%B!=0)
    59                 if(a[--r]<=now)
    60                     ++cnt;
    61             while(l<r)
    62             {
    63                 int b=l/B;
    64                 cnt+=upper_bound(bucket[b].begin(),bucket[b].end(),now)-bucket[b].begin();
    65                 l+=B;
    66             }
    67             if(cnt>=k)
    68             {
    69                 you=mid;
    70             }
    71             else
    72                 zuo=mid;
    73         }
    74         printf("%d
    ",num[you]);
    75     }
    76     return 0;
    77 }
  • 相关阅读:
    mac 安装Windows系统
    各种镜像源
    应用官方下载地址汇总
    centos7 升级openssh
    ubuntu16.04升级openssh
    腾讯云
    msdeploy 远程发布到lls
    Java Script 什么是闭包?
    JavaScript我的怀疑
    HTML 之 js是干什么的
  • 原文地址:https://www.cnblogs.com/quintessence/p/6891319.html
Copyright © 2011-2022 走看看