# hdu 6231 -- K-th Number(二分+尺取)

Problem Description
Alice are given an array

Input
The first line is the number of test cases.

For each test case, the first line contains three positive numbers

Output
For each test case, output a single line containing the

Sample Input
2
5 3 2
2 3 1 5 4
3 3 1
5 8 2

Sample Output
3
2

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
typedef long long LL;
const int N=1e5+5;
int a[N],b[N];

LL get(int x,int n,int k)
{
LL ans=0;
int pos=1;
int num=0;
for(int i=1;i<=n;i++)
{
if(a[i]>=x) num++;
if(num==k)
{
ans+=n-i+1;
while(a[pos]<x)
{
ans+=n-i+1;
pos++;
}
num--; pos++;
}
}
return ans;
}

int main()
{
int T; cin>>T;
while(T--)
{
int n,k;
LL m;
scanf("%d%d%lld",&n,&k,&m);
for(int i=1;i<=n;i++) scanf("%d",&a[i]), b[i]=a[i];
sort(b+1,b+n+1);
int num=unique(b+1,b+n+1)-(b+1);
int L=1,R=num;
while(L<=R)
{
int mid=(L+R)>>1;
LL tmp=get(b[mid],n,k);
if(tmp<m) R=mid-1;
else L=mid+1;
}
printf("%d\n",b[L-1]);
}
return 0;
}

posted @ 2017-11-12 14:44  茶飘香~  阅读(...)  评论(...编辑  收藏