HDU - 6231 K-th Number

Alice are given an array A[1..N]A[1..N] with NN numbers. 

Now Alice want to build an array BB by a parameter KK as following rules: 

Initially, the array B is empty. Consider each interval in array A. If the length of this interval is less than KK, then ignore this interval. Otherwise, find the KK-th largest number in this interval and add this number into array BB. 

In fact Alice doesn't care each element in the array B. She only wants to know the MM-th largest element in the array BB. Please help her to find this number.

Input

The first line is the number of test cases. 

For each test case, the first line contains three positive numbers N(1≤N≤105),K(1≤K≤N),MN(1≤N≤105),K(1≤K≤N),M. The second line contains NN numbers Ai(1≤Ai≤109)Ai(1≤Ai≤109). 

It's guaranteed that M is not greater than the length of the array B. 

Output

For each test case, output a single line containing the MM-th largest element in the array BB.

Sample Input

2
5 3 2
2 3 1 5 4
3 3 1
5 8 2

Sample Output

3
2

给你一个数组A 在每个可行的连续区间里找到第K大的数放入数组B,然后从数组B里找到第M大的数。

反过来思考,这个数就是在数组A里,作为第K大的数的时候的区间数为M。然后就可以二分了。统计区间个数用到了尺取法。

AC代码:

#include <cstdio>
#include <cstring>
#include <iostream>

using namespace std;

typedef long long LL;

int N,K;
LL M;

int a[100010];

bool judge(int x)
{
    LL ans=0;
    int num=0;
    int j=1;

    for(int i=1;i<=N;i++)
    {
        if(a[i]>=x)
            num++;
        if(num==K)
        {
            ans+=N-i+1;
            while(a[j]<x)
            {
                ans+=N-i+1;
                j++;
            }
            num--;
            j++;
        }
    }

    if(ans>=M)
        return true;
    return false;
}

int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        cin>>N>>K>>M;
        for(int i=1;i<=N;i++)
            cin>>a[i];

        int l=1,r=1000000000;
        int mid;
        while(l<r)
        {
            mid=(l+r)/2;
            if(judge(mid))
                l=mid+1;
            else
                r=mid;
        }
        cout<<l-1<<endl;
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值