Alice are given an array B. 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 B.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;
}

 

相关文章:

  • 2022-01-18
  • 2021-07-29
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-05-28
  • 2021-09-28
猜你喜欢
  • 2022-02-01
  • 2022-12-23
  • 2021-08-13
  • 2021-10-09
  • 2021-05-22
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案