Cod sursa(job #856860)

Utilizator BalcauIonutFMI-Balcau Ionut BalcauIonut Data 17 ianuarie 2013 00:10:51
Problema Statistici de ordine Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.07 kb
#include<cstdlib>
#include<ctime>
#include<fstream>
 
using namespace std;
ifstream f("sdo.in");
ofstream g("sdo.out");
  
int n, k, a[3000001];
  
void sort(int l,int r)
{
    if(l<r)
    {
        
        int rnd, val,x,y;
        rnd=rand()%(r-l+1)+l;
             
                  
        x=l;
        y=r;
        val=a[rnd];
 
        while(x<=y){
            while( a[x] < val )
                x++;
            while( a[y] > val )
                y--;
            if( x <= y ){
                int aux;
                aux=a[x];
                a[x]=a[y];
                a[y]=aux;
                x++;
                y--;
            }
        }
        if( ( x-y == 2 ) && ( k == x-1 ) )
            g<<a[x-1]<<" ";
        else
            if(k>=x) 
                sort(x,r);
            else sort(l,y);
      
    }
    else g<<a[l]<<" ";
}
          
          
  
  
int main(){
    f>>n>>k;
    srand ( time(NULL) );
    int i;
    for(i=1;i<=n;i++)
        f>>a[i];
      
    sort(1,n);
      
    return 0;
}