Cod sursa(job #1850940)

Utilizator teo.cons98Constantin Teodor-Claudiu teo.cons98 Data 19 ianuarie 2017 02:56:28
Problema Statistici de ordine Scor 90
Compilator cpp Status done
Runda Arhiva educationala Marime 0.98 kb
#include <iostream>
#include <fstream>
#include <algorithm>
#include <cstdlib>
using namespace std;
ifstream fin("sdo.in");
ofstream fout("sdo.out");

unsigned int n, k, v[3000001];

void quickSort(unsigned long long st, unsigned long long dr)
{
    unsigned int i, j, f;
    unsigned long long piv = (dr + st) / 2;

    piv = v[piv];
    i = st;
    j = dr;

    do
    {
        while(v[i] < piv and i <= dr)
        {
            ++i;
        }
        while(v[j] > piv and j >= st)
        {
            --j;
        }
        if(i <= j)
        {
            f = v[i];
            v[i] = v[j];
            v[j] = f;
            i++;
            j--;
        }
    }while(i <= j);

    if(j == k) fout<<v[j]<<endl;
    else if(j > k) quickSort(st, j);
    else quickSort(i, dr);

}

void citire()
{
    fin>>n>>k;
    for(unsigned int i = 1; i <= n; ++i)
    {
        fin>>v[i];
    }
}

int main()
{
    citire();
    quickSort(1, n);
}