HDU1899 Sum the K-th's

题解:

动态求第k大数问题。

此题a[i]的值比较大。

如果直接用map的话TLE,所以需要hash

具体做法是将a[i]排序并且unique。然后将a[i]的值映射到i下。

注意在最后求和时要换回来即可

代码:

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<map>
#include<set>
using namespace std;
using namespace std;
#define pb push_back
#define mp make_pair
#define se second
#define fs first
#define ll long long
#define CLR(x) memset(x,0,sizeof x)
#define SZ(x) ((int)(x).size())
#define FOR(it,c) for(__typeof((c).begin()) it=(c).begin();it!=(c).end();it++)
typedef pair<int,int> P;
const double eps=1e-9;
const int maxn=100010;
const int N=1e9;
const int mod=1e9+7;

ll read()
{
    ll x=0,f=1;char ch=getchar();
    while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();}
    while(ch>=‘0‘&&ch<=‘9‘){x=x*10+ch-‘0‘;ch=getchar();}
    return x*f;
}
//-----------------------------------------------------------------------------

int n,m,k,T,tot,b;
int a[maxn],t[maxn],c[maxn];

int lowbit(int x){return x&-x;}

void add(int x,int v){
    while(x<=tot){
        c[x]+=v;
        x+=lowbit(x);
    }
}

int sum(int x){
    int cnt=0;
    while(x){
        cnt+=c[x];
        x-=lowbit(x);
    }
    return cnt;
}

int bs(){
    int l=0,r=tot;
    int goal=-1;
    while(l<=r){
        int m=(l+r)>>1;
        if(sum(m)>=k){
            goal=m;
            r=m-1;
        }
        else l=m+1;
    }
    return t[goal];
}

int Bin(int v){
    int l=1,r=tot;
    while(l<=r){
        int m=(l+r)>>1;
        if(v==t[m]) return m;
        if(v>t[m]) l=m+1;
        else       r=m-1;
    }
}

int main(){
    T=read();
    while(T--){
        n=read();m=read();k=read();
        for(int i=1;i<=n;i++){
            a[i]=read();
            t[i]=a[i];
        }
        sort(t+1,t+n+1);
        tot=0;
        for(int i=1;i<=n;i++){
            if(i==1||t[i]!=t[i-1]){
                t[++tot]=t[i];
                c[tot]=0;
            }
        }
        for(int i=1-m+n;i<=n;i++){
            b=Bin(a[i]);
            add(b,1);
        }
        for(int i=2;i<=1+m;i++){
            b=Bin(a[i]);
            add(b,1);
        }
        int ans=0;
        ans+=bs();
        ans%=mod;
        int r,l;
        for(int i=2;i<=n;i++){
            b=Bin(a[i-1]);
            add(b,1);
            b=Bin(a[i]);
            add(b,-1);
            r=i+m;
            if(r>n) r-=n;
            b=Bin(a[r]);
            add(b,1);
            l=i-1-m;
            if(l<=0) l+=n;
            b=Bin(a[l]);
            add(b,-1);
            ans+=bs();
            ans%=mod;
        }
        printf("%d\n",ans);
    }
    return 0;
}

HDU1899 Sum the K-th's

时间: 2024-07-28 17:42:48

HDU1899 Sum the K-th's的相关文章

560. Subarray Sum Equals K

Given an array of integers and an integer k, you need to find the total number of continuous subarrays whose sum equals to k. Example 1: Input:nums = [1,1,1], k = 2 Output: 2 Note: The length of the array is in range [1, 20,000]. The range of numbers

325. Maximum Size Subarray Sum Equals k

Given an array nums and a target value k, find the maximum length of a subarray that sums to k. If there isn't one, return 0 instead. Note: The sum of the entire nums array is guaranteed to fit within the 32-bit signed integer range. Example 1: Given

[Locked] Maximum Size Subarray Sum Equals k

Example 1: Given nums = [1, -1, 5, -2, 3], k = 3,return 4. (because the subarray [1, -1, 5, -2] sums to 3 and is the longest) Example 2: Given nums = [-2, -1, 2, 1], k = 1,return 2. (because the subarray [-1, 2] sums to 1 and is the longest) Follow U

算法(10)Subarray Sum Equals K

题目:在数组中找到一个子数组,让子数组的和是k. 思路:先发发牢骚,这两天做题是卡到不行哇,前一个题折腾了三天,这个题上午又被卡住,一气之下,中午睡觉,下午去了趟公司,竟然把namespace和cgroup的架构给搞懂了!所以晚上再来攻克这个问题!上午的做法是这样的,设置一个fast指针,一个slow指针,当[slow,fast]中的值大于k的时候,fast++,反之slow++,这种错误的解法误以为数组是有序的,所以是行不通的,那么这道题的解法是什么呢?然后写了下O(n^3)的解法,果不其然,

[leetcode 560. Subarray Sum Equals K]

Given an array of integers and an integer k, you need to find the total number of continuous subarrays whose sum equals to k. Example 1: Input:nums = [1,1,1], k = 2 Output: 2 Note: The length of the array is in range [1, 20,000]. The range of numbers

[?]*Maximum Size Subarray Sum Equals k

Given an array nums and a target value k, find the maximum length of a subarray that sums to k. If there isn't one, return 0 instead. Example 1: Given nums = [1, -1, 5, -2, 3], k = 3,return 4. (because the subarray [1, -1, 5, -2] sums to 3 and is the

Leetcode: Maximum Size Subarray Sum Equals k

Given an array nums and a target value k, find the maximum length of a subarray that sums to k. If there isn't one, return 0 instead. Example 1: Given nums = [1, -1, 5, -2, 3], k = 3, return 4. (because the subarray [1, -1, 5, -2] sums to 3 and is th

[LeetCode] Maximum Size Subarray Sum Equals k 最大子数组之和为k

Given an array nums and a target value k, find the maximum length of a subarray that sums to k. If there isn't one, return 0 instead. Example 1: Given nums = [1, -1, 5, -2, 3], k = 3, return 4. (because the subarray [1, -1, 5, -2] sums to 3 and is th

Map-560. Subarray Sum Equals K

Given an array of integers and an integer k, you need to find the total number of continuous subarrays whose sum equals to k. Example 1: Input:nums = [1,1,1], k = 2 Output: 2 Note: The length of the array is in range [1, 20,000]. The range of numbers