Pagini recente » Diferente pentru problema/sumtree intre reviziile 33 si 8 | Monitorul de evaluare | Monitorul de evaluare | Monitorul de evaluare | Cod sursa (job #3324733)
#include <iostream>
#include <vector>
#include <fstream>
using namespace std;
ifstream fin("sdo.in");
ofstream fout("sdo.out");
vector<int>nr;
int n, k;
int pivotare(int st, int dr)
{
int i = st;
int pivot = nr[dr];
for(int j = st; j < dr; j++)
{
if(nr[j] <= pivot)
{
swap(nr[j], nr[i]);
i++;
}
}
swap(nr[dr], nr[i]);
return i;
}
int QS(int st, int dr)
{
if(st<dr)
{
int indice = pivotare(st, dr);
if(k == indice)
return nr[k];
if(k < indice)
{
QS(st, indice-1);
}
else
QS(indice+1, dr);
}
}
int main()
{
fin >> n >> k;
int x;
nr.push_back(99999999);
for(int i = 1; i <= n; i++)
{
fin >> x;
nr.push_back(x);
}
fout << QS(1, n);
return 0;
}