zoukankan      html  css  js  c++  java
  • 平方分割poj2104K-th Number



    K-th Number
    Time Limit: 20000MS   Memory Limit: 65536K
    Total Submissions: 59798   Accepted: 20879
    Case Time Limit: 2000MS

    Description

    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

    题意:给出一列数组和几组查询,对于每个查询(i,j,k),要求输出a(i)~a(j)的升序排列的第k大数。

    思路:挑战者上的题目,上面采用的是平方分割法,总体来讲,线段树的复杂度肯定是比平方分割的复杂度更小,但平方分割的实现更加简单易懂。

    但是敲了一遍书上的代码,不知道是不是手残,敲完以后总有个别数据对不上答案(心累,调了一个小时不知所以然)。


    给出AC代码

    #include <iostream>
    #include<cstdio>
    #include<algorithm>
    #include<vector>
    using namespace std;
    
    #define MAXN 100010
    #define MAXM 5010
    #define B 1000   //桶的大小
    
    //输入
    int n,m;
    int a[MAXN];
    int L[MAXM],R[MAXM],K[MAXM];
    
    int num[MAXN];  //对A排序后的结果
    vector<int> bucket[MAXN/B];  //每个桶排序后的结果
    
    void solve()
    {
        for(int i=0;i<n;i++)
        {
            bucket[i/B].push_back(a[i]);
            num[i]=a[i];
        }
        sort(num,num+n);
        for(int i=0;i<n/B;i++)
            sort(bucket[i].begin(),bucket[i].end());
        for(int i=0;i<m;i++)
        {
            //求[L,R]区间的第K个数
            int l=L[i]-1,r=R[i],k=K[i];
            int left=-1,right=n-1,mid;
            while(right-left>1)
            {
                mid=(left+right)/2;
                int x=num[mid];
                int tl=l,tr=r,c=0;
    
                //区间两端多出的部分
                while(tl<tr&&tl%B!=0){
                    if(a[tl++]<=x) c++;
                }
                while(tl<tr&&tr%B!=0){
                    if(a[--tr]<=x) c++;
                }
    
                //对每个桶进行计算
                while(tl<tr)
                {
                    int  b=tl/B;
                    c+=upper_bound(bucket[b].begin(),bucket[b].end(),x)-bucket[b].begin();
                    tl+=B;
                }
    
                if(c>=k)
                    right=mid;
                else
                    left=mid;
            }
            printf("%d
    ",num[right]);
        }
    }
    
    int main()
    {
        scanf("%d%d",&n,&m);
        for(int i=0;i<n;i++)
            scanf("%d",&a[i]);
        for(int i=0;i<m;i++)
            scanf("%d%d%d",&L[i],&R[i],&K[i]);
        solve();
        return 0;
    }
    






  • 相关阅读:
    Silverlight4 打印 生成文件过大解决
    清理 Visual Studio 工具箱 的冗杂控件(第三方控件卸载不完全)
    SQL2005SP4生成数据库脚本视图依赖顺序错误问题
    SQL Server 2005 企业版没有 Management Studio管理工具
    Excel Reader 轻量级
    开发托管ActiveX或第三方程序托管插件时调试问题解决方法
    Entity Framework 从数据库生成模型丢失数据库文档不完美解决方案
    今天写了个很蛋疼的sql语句
    WinForm 内嵌 Office 文档 解决方案测试(非DSOFRAME 纯C#代码,网上独一份)
    生产者、消费者问题之闹钟
  • 原文地址:https://www.cnblogs.com/bryce1010/p/9387336.html
Copyright © 2011-2022 走看看