HDU 6231 (K-th Number)
题目链接:https://cn.vjudge.net/problem/HDU-6231
思路:二分+双指针;
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <math.h>
#include <algorithm>
using namespace std;
typedef long long int LL;
const int INF=2e9+1e8;
const int maxn=1e5+100;
int a[maxn],b[maxn];
int n,k;
LL m;
LL calc(int x) //计算区间第k大的数大于等于x的区间个数
{
LL ans=0;
int l=0,r=-1,num=0;
while(r<n)
{
if(num<k)
{
if(a[r+1]>=x) num++;
r++;
}
else
{
if(num==k) ans+=n-r;
if(a[l]>=x) num--;
l++;
}
}
return ans;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d%d%lld",&n,&k,&m);
for(int i=0;i<n;i++)
scanf("%d",&a[i]),b[i]=a[i];
sort(b,b+n);
int len=unique(b,b+n)-b;
int l=0,r=len-1,ans=-1;
while(l<=r)
{
int mid=(l+r)>>1;
if(calc(b[mid])>=m) ans=b[mid],l=mid+1;
else r=mid-1;
}
printf("%d\n",ans);
}
return 0;
}