Pagini recente » Cod sursa (job #2168581) | Cod sursa (job #2720971) | Cod sursa (job #3220480) | Cod sursa (job #3243309) | Cod sursa (job #3141432)
#include <fstream>
#include <algorithm>
using namespace std;
int n, V[3000001];
ifstream fin("x.in");
ofstream fout("x.out");
int Nth_element(int V[], int st, int dr, int k){
if(st <= dr){
int position = (st + dr) / 2;
int pivot = V[position];
int i = st - 1;
for(int j = st; j <= dr; ++j)
if(V[j] < pivot){
i++;
if(i == position)
position = j;
swap(V[i], V[j]);
}
swap(V[i + 1], V[position]);
if(i + 1 == k)
return V[i + 1];
else
if(i + 1 > k)
return Nth_element(V, st, i, k);
else
return Nth_element(V, i + 2, dr, k);
}
return 0;
}
int main()
{
int n, k;
fin >> n >> k;
for(int i = 1; i <= n; ++i){
fin >> V[i];
}
fout << Nth_element(V, 1, n, k);
}