Pagini recente » Cod sursa (job #1906156) | Cod sursa (job #379114) | Cod sursa (job #1896219) | Cod sursa (job #376611) | Cod sursa (job #1894532)
#include <bits/stdc++.h>
using namespace std;
int N, K;
vector < int > a;
ifstream f("sdo.in");
ofstream g("sdo.out");
void nthElement(int left, int right) {
int i = left, j = right;
int pivot = left + rand() % (right - left + 1);
while (i <= j) {
while (a[i] < a[pivot]) {
++i;
}
while (a[j] > a[pivot]) {
--j;
}
if (i <= j) {
swap(a[i], a[j]);
++i;
--j;
}
}
if (left < j && j >= K) {
nthElement(left, j);
} else if (i < right && j < K) {
nthElement(i, right);
}
}
int main() {
srand(time(NULL));
f >> N >> K;
a.resize(N);
--K;
for (int i = 0; i < N; ++i) {
f >> a[i];
}
nthElement(0, N - 1);
g << a[K] << '\n';
// for (int i = 0; i < N; ++i) {
// g << a[i] << ' ';
// }
return 0;
}