Pagini recente » Cod sursa (job #168080) | Cod sursa (job #2655712) | Cod sursa (job #2951172) | Cod sursa (job #2054659) | Cod sursa (job #1255064)
// FMI - Grupa 135 - Semigrupa 2 - Hareza Andrei
// Include
#include <fstream>
#include <cstdlib>
using namespace std;
// Constante
const int sz = 3000001;
// Functii
template<class T> T findPosElement(T *values, int left, int right, int pos);
template<class T> int part(T *values, int left, int right);
// Variabile
ifstream in("sdo.in");
ofstream out("sdo.out");
int num, pos;
int values[sz];
// Main
int main()
{
srand(3110);
in >> num >> pos;
for(int i=1 ; i<=num ; ++i)
in >> values[i];
out << findPosElement(values, 1, num, pos) << '\n';
in.close();
out.close();
return 0;
}
template<class T> T findPosElement(T *values, int left, int right, int pos)
{
if(left == right)
return values[left];
int point = part(values, left, right);
if(pos <= point)
return findPosElement(values, left, point, pos);
else
return findPosElement(values, point+1, right, pos);
}
template<class T> int part(T *values, int left, int right)
{
int currentLeft = left;
int currentRight = right;
T point = values[rand()%(right-left+1) + left];
for(;;)
{
while(values[currentLeft] < point)
++currentLeft;
while(point < values[currentRight])
--currentRight;
if(currentLeft < currentRight)
swap(values[currentLeft++], values[currentRight--]);
else
return currentRight;
}
return 0;
}