Pagini recente » Cod sursa (job #1794257) | Cod sursa (job #849019) | Cod sursa (job #1419050) | Cod sursa (job #2572613) | Cod sursa (job #1513284)
#include <fstream>
#include <cstdlib>
using namespace std;
int A[4000000];
int nth(int *A, int n, int k) {
if(n == 1) return A[1];
swap(A[n], A[rand() % n + 1]);
int poz = 1;
for(int i=1; i<n; i++) {
if(A[i] <= A[n])
swap(A[i], A[poz++]);
}
swap(A[n], A[poz]);
if(k == poz) return A[poz];
if(poz > k) return nth(A, poz-1, k);
return nth(A+poz+1, n-poz-1, k-poz);
}
int main() {
ifstream fin("sdo.in");
ofstream fout("sdo.out");
int n, k;
fin>>n>>k;
for(int i=1; i<=n; i++)
fin>>A[i];
fout<<nth(A, n, k);
return 0;
}