Cod sursa(job #2078106)

Utilizator Andrei243Nitu Mandel Andrei Andrei243 Data 28 noiembrie 2017 22:03:07
Problema Statistici de ordine Scor 20
Compilator cpp Status done
Runda Arhiva educationala Marime 1.01 kb
#include <iostream>
#include <stdlib.h>
#include <fstream>
#include <algorithm>
#include <time.h>

using namespace std;
int v[3000000];
ifstream in("sdo.in");
ofstream out("sdo.out");
void swap(int &a,int &b)
{
    a=a^b;
    b=a^b;
    a=a^b;

}


int partition ( int st, int dr)
{
    int i=st-1;
    int poz_piv=st+rand()%(dr-st+1);
    swap(v[poz_piv],v[dr]);

    int pivot=v[dr];
    int j=dr+1;
    int loc;
    while(i<j)
    {

        while(v[++i]<pivot);
        while(v[--j]>pivot);
        if(i>=j)loc=j;
        else
            swap(v[i],v[j]);


    }






    return loc;
}



int quickSort( int st, int dr,int k)
{
    if (st == dr)return v[st];








    int pi = partition( st, dr);
    if(k+st-1<=pi)return quickSort( st, pi,k);
    else return quickSort( pi + 1, dr,k-pi+st-1);






}


int main()
{srand(time(NULL));
    int n,k;
    in>>n>>k;
    for(int i=1; i<=n; i++)in>>v[i];
    out<<quickSort(1,n,k);
    return 0;
}