Pagini recente » Cod sursa (job #2904653) | Clasament ring1 | Istoria paginii runda/simulare-cartita-18b/clasament | Cod sursa (job #2448924) | Cod sursa (job #1246572)
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <ctime>
using namespace std;
const int NMax = 3000010;
int a[NMax];
int N, K;
void Read()
{
ifstream f("sdo.in");
f >> N >> K;
for (int i = 1; i <= N; ++ i)
f >> a[i];
f.close();
}
int sdo(const int st, const int dr, const int k)
{
if (st == dr)
return a[st];
int poz = st + (rand() % (dr - st + 1));
int pivot = a[poz], i, j;
i = st, j = dr;
while (i <= j)
{
while (a[i] < pivot)
++ i;
while (a[j] > pivot)
-- j;
if (i <= j)
{
swap(a[i], a[j]);
++ i, -- j;
}
}
if (j - st + 1 >= k)
return sdo(st, j, k);
else
return sdo(j+1, dr, k - j + st - 1);
}
void Write()
{
ofstream g ("sdo.out");
g << sdo(1, N, K) << "\n";
g.close();
}
int main()
{
srand(time(NULL));
Read();
Write();
return 0;
}