题解 | #选数#
选数
https://www.nowcoder.com/practice/50636077fde843c09bf915d0aef57a99
#include <bits/stdc++.h> using namespace std; int main(){ int n,k; long long s; cin >> n >> k >> s; vector<int> a(n); for(int i = 0; i < n; i++) cin >> a[i]; sort(a.begin(),a.end()); // 二分查找最大的值中最小的那个! int ans(-1); int l = 0, r = n-1; while(l < r){ int mid = l + ((r-l)>>1); long long sum(0); int tl = max(0,mid-k+1),tr = mid; for(int i = tl; i <= tr; i++) sum += a[i]; if(sum >= s) ans = a[mid],r = mid; else l = mid+1; } cout << ans << endl; return 0; }