Mai intai trebuie sa te autentifici.

Cod sursa(job #2308049)

Utilizator corvinus2003Corvin Ghita corvinus2003 Data 26 decembrie 2018 11:52:11
Problema Statistici de ordine Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.98 kb
#include <fstream>
#include <algorithm>
#include <cstdlib>

using namespace std;

ifstream cin ("sdo.in");
ofstream cout ("sdo.out");

const int nmax = 3000001;
int n, k, a[nmax];

int AlegRandPivot(int st, int dr)
{
    int random = st + rand() % (dr - st);
    return random;
}

int partitie(int st, int dr)
{
    int pivot = a[dr];
    int i, j;
    i = st - 1;
    for (j = st; j < dr; ++j)
        if (a[j] < pivot)
        {
            i++;
            swap(a[i], a[j]);
        }
    swap(a[i + 1], a[dr]);
    return i + 1;
}

int fc(int st, int dr)
{
    if (st >= dr)
        return st;
    int pivot = AlegRandPivot(st, dr);
    swap(a[pivot], a[dr]);
    int poz = partitie(st, dr);
    if (poz > k)
        return fc(st, poz - 1);
    if (poz < k)
        return fc(poz + 1, dr);
}

int main()
{
    cin >> n >> k;
    for (int i = 1; i <= n; ++i)
        cin >> a[i];
    int pozitie = fc(1, n);
    cout << a[pozitie];
    return 0;
}