Pagini recente » Cod sursa (job #3283595) | Cod sursa (job #2448119) | Cod sursa (job #2491335) | Cod sursa (job #2271682) | Cod sursa (job #479520)
Cod sursa(job #479520)
#include <iostream>
#include <fstream>
#include <sstream>
#include <ctime>
#include <cstdlib>
using namespace std;
void order(int *a, int n, int k)
{
int j, st, dr;
bool dir;
while(1)
{
int j = rand() % n;
swap(a[0], a[j]);
st = 0, dr = n-1, dir = false;
while (st < dr)
{
if (a[st] > a[dr])
swap(a[st], a[dr]), dir = !dir;
if(dir)
++ st;
else
-- dr;
}
j = st;
if(j == k)
return;
if(j < k)
a += j, n -= j, k -= j;
else
n = j;
}
}
int a[3000000];
char buf[1000000];
int main()
{
srand(time(NULL));
ifstream cin("sdo.in");
cin.rdbuf()->pubsetbuf(buf, 1000000);
ofstream fout("sdo.out");
int k, n;
cin >> n >> k;
-- k;
for(int i=0;i<n;++i)
cin >> a[i];
order(a, n, k);
fout << a[k];
return 0;
}