首页 > 其他 > 详细

HDU - 6231:K-th Number (不错的二分)

时间:2018-09-24 22:38:32      阅读:73      评论:0      收藏:0      [点我收藏+]

标签:false   parameter   initial   ron   mes   put   this   eas   content   

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.

InputThe first line is the number of test cases. 

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

It‘s guaranteed that M is not greater than the length of the array B. 
OutputFor 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

题意:把所有区间从小到大的第K大取出来再排序,求第M大。

思路:把所有数拿进去二分,大于等于M的数则check成功,R=Mid+1;

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
long long n,k,m,a[100010],b[100010],T,ans; 
inline bool pd(long long x){
    int cnt=0;long long tot=0;
    for(register int i=1,j=1;i<=n;i++){
        while(cnt<k&&j<=n)cnt+=a[j++]>=x;
        if(cnt<k)break;
        tot+=n-j+2;
        cnt-=a[i]>=x;
    }
    return tot>=m;
}
int main(){
    for(scanf("%lld",&T);T--;){
        scanf("%lld%lld%lld",&n,&k,&m);ans=0;
        for(register int i=1;i<=n;i++)scanf("%lld",a+i),b[i]=a[i];
        sort(b+1,b+1+n);
        long long l=1,r=n,mid;
        while(l<=r){
            mid=(l+r)>>1;
            if(pd(b[mid]))ans=mid,l=mid+1;
            else r=mid-1;
        }
        printf("%lld\n",b[ans]);
    }
    return 0;
}

 

HDU - 6231:K-th Number (不错的二分)

标签:false   parameter   initial   ron   mes   put   this   eas   content   

原文:https://www.cnblogs.com/hua-dong/p/9696915.html

(0)
(0)
   
举报
评论 一句话评论(0
0条  
登录后才能评论!
© 2014 bubuko.com 版权所有 鲁ICP备09046678号-4
打开技术之扣,分享程序人生!
             

鲁公网安备 37021202000002号