Cod sursa(job #1022354)

Utilizator buzu.tudor67Tudor Buzu buzu.tudor67 Data 5 noiembrie 2013 11:10:53
Problema Statistici de ordine Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.84 kb
#include<fstream>
#define maxn 3000005
using namespace std;

ifstream fi("sdo.in");
ofstream fo("sdo.out");

int i,n,k,a[maxn];

void swap2(int &x,int &y){
     int aux;
     aux=x; x=y; y=aux;
}

void statistici(int left,int right){
     int i,j,piv;
     piv=a[(left+right)/2];
     i=left; j=right;
     
     while (i<j) {
                  while (a[i]<piv) i++;
                  while (a[j]>piv) j--;
                  if (i<=j) {
                             swap2(a[i],a[j]);
                             i++; j--;
                            }
                 }
     if (k<=j && left<j) statistici(left,j);
     if (k>=i && i<right) statistici(i,right);
}

int main(void){
    fi>>n>>k;
    for(i=1;i<=n;i++) fi>>a[i];
    
    statistici(1,n);

    fo<<a[k];
    fi.close();
    fo.close();
    return 0;
}