Pagini recente » Cod sursa (job #436656) | Cod sursa (job #3330998) | Cod sursa (job #1364855) | Cod sursa (job #772018) | Cod sursa (job #3333442)
#include <fstream>
using namespace std;
ifstream fin("sdo.in");
ofstream fout("sdo.out");
static int a[3000005];
int main() {
ios::sync_with_stdio(false);
fin.tie(nullptr);
int n, k;
fin >> n >> k;
k--;
for (int i = 0; i < n; i++) fin >> a[i];
int L = 0, R = n - 1;
while (true) {
int pivot = a[L + rand() % (R - L + 1)];
int i = L, j = L, t = R;
while (j <= t) {
if (a[j] < pivot) {
swap(a[i], a[j]);
i++;
j++;
} else if (a[j] > pivot) {
swap(a[j], a[t]);
t--;
} else j++;
}
if (k < i) R = i - 1;
else if (k > t) L = t + 1;
else {
fout << pivot << "\n";
return 0;
}
}
}