• hdu 4217 Data Structure? 树状数组求第K小


    Data Structure?

    Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)


    Problem Description
    Data structure is one of the basic skills for Computer Science students, which is a particular way of storing and organizing data in a computer so that it can be used efficiently. Today let me introduce a data-structure-like problem for you.
    Original, there are N numbers, namely 1, 2, 3...N. Each round, iSea find out the Ki-th smallest number and take it away, your task is reporting him the total sum of the numbers he has taken away.
     
    Input
    The first line contains a single integer T, indicating the number of test cases.
    Each test case includes two integers N, K, K indicates the round numbers. Then a line with K numbers following, indicating in i (1-based) round, iSea take away the Ki-th smallest away.

    Technical Specification
    1. 1 <= T <= 128
    2. 1 <= K <= N <= 262 144
    3. 1 <= Ki <= N - i + 1
     
    Output
    For each test case, output the case number first, then the sum.
     
    Sample Input
    2 3 2 1 1 10 3 3 9 1
     
    Sample Output
    Case 1: 3 Case 2: 14
     
    Author
    iSea@WHU
     
    Source
     思路:taobanzi;
    #include<bits/stdc++.h>
    using namespace std;
    #define ll long long
    #define pi (4*atan(1.0))
    #define eps 1e-14
    const int N=3e5+10,M=4e6+10,inf=1e9+10,mod=1e9+7;
    const ll INF=1e18+10;
    int tree[N],n,k;
    int lowbit(int x)
    {
        return x&-x;
    }
    void update(int x,int change)
    {
        while(x<=n)
        {
            tree[x]+=change;
            x+=lowbit(x);
        }
    }
    int k_thfind(int K)//树状数组求第K小
    {
        int sum=0;
        for(int i=18;i>=0;i--)
        {
            if(sum+(1<<i)<=n&&tree[sum+(1<<i)]<K)
            {
                K-=tree[sum+(1<<i)];
                sum+=1<<i;
            }
        }
        return sum+1;
    }
    int main(){
        int T,cas=1;
        scanf("%d",&T);
        while(T--)
        {
            memset(tree,0,sizeof(tree));
            scanf("%d%d",&n,&k);
            for(int i=1;i<=n;i++)
                update(i,1);
            ll ans=0;
            for(int i=0;i<k;i++)
            {
                int z;
                scanf("%d",&z);
                int v=k_thfind(z);
                ans+=v;
                update(v,-1);
            }
            printf("Case %d: %lld
    ",cas++,ans);
        }
        return 0;
    }
  • 相关阅读:
    Bamboo CI 使用的一些记录
    fork 与 branch、clone 的区别
    使用--ignore-scripts解决npm/yarn安装依赖失败问题
    gyp ERR! find VS gyp ERR! find VS msvs_version not set from command line or npm config gyp ERR! find
    sdk manager 打不开解决方法
    appium
    北京幼升小
    MySQL中limit的问题
    初始社保费管理客户端的单位编号
    flutter BackdropFilter的毛玻璃效果使用
  • 原文地址:https://www.cnblogs.com/jhz033/p/5931301.html
Copyright © 2020-2023  润新知