Pagini recente » Cod sursa (job #1150856) | Cod sursa (job #1292735) | Cod sursa (job #1179528) | Cod sursa (job #262448) | Cod sursa (job #1549488)
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 3 * 1e6 + 7;
int N, K;
int A[MAXN];
int sdo(int st, int fn) {
int pivot = A[st + rand() % (fn - st + 1)];
int actst = st, actfn = fn;
while(true) {
while(A[actst] < pivot)
++actst;
while(A[actfn] > pivot)
--actfn;
if(actst < actfn)
swap(A[actst], A[actfn]);
else
return actfn;
}
return 0;
}
void solve(int st, int fn, int step) {
if(st == fn)
return;
int num = sdo(st, fn), cnt = num - st + 1;
if(cnt >= step)
solve(st, num, step);
else
solve(num + 1, fn, step - cnt);
}
void Read() {
srand(time(0));
ifstream fin("sdo.in");
fin >> N >> K;
for(int i = 1; i <= N; ++i)
fin >> A[i];
}
int main() {
Read();
solve(1, N, K);
ofstream("sdo.out") << A[K];
return 0;
}