K-th Number
K-th Number
https://ac.nowcoder.com/acm/problem/14301
题意
思路
Code:
#include<bits/stdc++.h> using namespace std; typedef long long ll; ll read(){ ll x, f = 1; char ch; while(ch = getchar(), ch < '0' || ch > '9') if(ch == '-') f = -1; x = ch - '0'; while(ch = getchar(), ch >= '0' && ch <= '9') x = x * 10 + ch - 48; return x * f; } int a[250005],s[250005],n,k; ll m; bool check(int x) { int l=0; ll sum=0; for(int i=1;i<=n;++i) s[i]=s[i-1]+(a[i]>=x); for(int i=k;i<=n;++i) { while(s[i]-s[l]==k) ++l; sum+=l; } if(sum>=m) return true; else return false;//x偏大 } int main() { int t=read(); while(t--) { n=read(),k=read(),m=read(); for(int i=1;i<=n;++i) a[i]=read(); int l=1,r=1e9; while(l<=r) { int mid=(l+r)>>1; if(!check(mid)) r=mid-1; else l=mid+1; } printf("%d\n",r); } return 0; }
牛客算法竞赛入门课第三节例题、习题 文章被收录于专栏
~很懒