Pagini recente » Cod sursa (job #3042214) | Cod sursa (job #1346355) | Monitorul de evaluare | Cod sursa (job #1369673) | Cod sursa (job #2823193)
#include <bits/stdc++.h>
using namespace std;
bool containsAtLeastKValuesLessOrEqual(vector<int>& v, const int& K, const int& THRESHOLD){
int count = 0;
for(int val: v){
if(val <= THRESHOLD){
count += 1;
}
if(count == K){
return true;
}
}
return false;
}
int main(){
ifstream cin("sdo.in");
ofstream cout("sdo.out");
int n, k;
cin >> n >> k;
vector<int> v(n);
for(int i = 0; i < n; ++i){
cin >> v[i];
}
int minVal = v[0];
int maxVal = v[0];
for(int val: v){
minVal = min(minVal, val);
maxVal = max(maxVal, val);
}
int l = minVal;
int r = maxVal;
while(l != r){
int mid = (l + r) / 2;
if(containsAtLeastKValuesLessOrEqual(v, k, mid)){
r = mid;
}else{
l = mid + 1;
}
}
cout << r;
cin.close();
cout.close();
return 0;
}