Cod sursa(job #1380227)

Utilizator pavlov.ionPavlov Ion pavlov.ion Data 6 martie 2015 23:56:55
Problema Statistici de ordine Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.82 kb
#include<fstream>
#include<ctime>
#include<algorithm>
using namespace std;
#define MAXN 3000005
int N,k,A[MAXN];
int pivotIndex,pivotValue,storeIndex;
ifstream cin("sdo.in");
ofstream cout("sdo.out");
int partitionare(int lower,int upper){
    srand(time(NULL));
    pivotIndex=lower+(rand()%(upper-lower+1));
    pivotValue=A[pivotIndex];
    swap(A[pivotIndex],A[upper]);
    storeIndex=lower;
    for(int i=lower;i<upper;i++)
        if(A[i]<pivotValue){
            swap(A[i],A[storeIndex]);
            storeIndex++;
        }
    swap(A[upper],A[storeIndex]);
    if(storeIndex==k)
        return A[storeIndex];
    if(storeIndex<k)
        return partitionare(storeIndex+1,upper);
    else
        return partitionare(lower,storeIndex-1);
}
int main(){
cin>>N>>k;
for(int i=1;i<=N;i++)
        cin>>A[i];
cout<<partitionare(1,N);

}