K-th Number
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 3111 Accepted Submission(s): 1126
Now Alice want to build an array B by a parameter K as following rules:
Initially, the array B is empty. Consider each interval in array A. If the length of this interval is less than K, then ignore this interval. Otherwise, find the K-th largest number in this interval and add this number into array B.
In fact Alice doesn't care each element in the array B. She only wants to know the M-th largest element in the array B. Please help her to find this number.
For each test case, the first line contains three positive numbers N(1≤N≤105),K(1≤K≤N),M. The second line contains N numbers Ai(1≤Ai≤109).
It's guaranteed that M is not greater than the length of the array B.
#include<bits/stdc++.h>
#define ll long long
#define inf 0x3f3f3f3f
#define mst(head,x,n) memset(head+1,x,n*sizeof(head[0]))
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define dep(i,a,b) for(int i=(a);i>=(b);i--)
using namespace std;
const int maxn=1e5+5;
//const double pi=acos(-1.0);
//const double eps=1e-9;
//const ll mo=1e9+7;
ll n,m,k;
ll ans,tmp,cnt,fg,tot;
vector<int>vc;
ll a[maxn],hs[maxn],b[maxn];
template <typename T>
inline void read(T &X){
X=0;int w=0; char ch=0;
while(!isdigit(ch)) {w|=ch=='-';ch=getchar();}
while(isdigit(ch)) X=(X<<3)+(X<<1)+(ch^48),ch=getchar();
if(w) X=-X;
}
int jud(int mid){
vc.clear();
ll sum=0;
vc.push_back(0);
rep(i,1,n){
if(b[i]>=mid) vc.push_back(i);
}
int up=vc.size()-1;
rep(i,0,up){
if(i<k) continue;
sum+=(ll)(vc[i-k+1]-vc[i-k])*(ll)(n-vc[i]+1);
}
return sum>=m;
}
int main(){
int T,cas=1;
read(T);
while(T--)
{
read(n);read(k);read(m);
rep(i,1,n) {read(a[i]);hs[i]=a[i];}
sort(hs+1,hs+1+n);
tot=unique(hs+1,hs+1+n)-hs-1;
rep(i,1,n){
b[i]=lower_bound(hs+1,hs+1+tot,a[i])-hs;
}
int l=1,r=tot,ans=0;
while(l<=r){
int mid=(l+r)>>1;
if(jud(mid)) {
ans=mid;
l=mid+1;
}
else r=mid-1;
}
printf("%d\n",hs[ans]);
}
return 0;
}