Cod sursa(job #1301588)

Utilizator RaileanuCristian Raileanu Raileanu Data 26 decembrie 2014 10:18:51
Problema Statistici de ordine Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.74 kb
#include <fstream>
#include <stdlib.h>
#include <time.h>
using namespace std;
#define MX 3000010
int a[MX], n,k,i;
ifstream f1("sdo.in");
ofstream f2("sdo.out");

int nth(int st, int dr, int k)
{
    if (st>=dr) return a[st];

    int piv=st+ rand()%(dr-st+1);
    int v= a[piv], i=st, j=dr;

    while (i<=j )
    {   while (a[i]<v && i<dr ) i++;
        while (a[j]>v && j>st) j--;
        if (i<=j)
        { swap(a[i],a[j] );
          i++; j--;
        }
    }

    if (k<=j && j>=st )
        return nth(st,j,k);
    if (j+1<=dr )
        return nth(j+1,dr,k);
}


int main()
{
    f1>>n>>k;
    for (i=1;i<=n;i++)
        f1>>a[i];

    srand(time(NULL));

    f2<<nth(1,n,k);
    f2.close();
    return 0;
}