Cod sursa(job #586424)

Utilizator drywaterLazar Vlad drywater Data 1 mai 2011 01:10:32
Problema Statistici de ordine Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.84 kb
#include <fstream>
#include <ctime>
#include <cstdlib>
using namespace std;
int n,v[3000005],k,i;
int caut(int x,int y,int k)
{
    if (x==y) return 0;
    int p=v[x+(rand()%(y-x+1))];
    int i=x-1,j=y+1,aux,ok=0;
    while (!ok)
    {
        do
        {
            i++;
        }
        while (v[i]<p);
        do
        {
            j--;
        }
        while (v[j]>p);
        if (i<j)
        {
            aux=v[i];
            v[i]=v[j];
            v[j]=aux;
        }
        else ok=1;
    }
    int t=j-x+1;
    if (t>=k)
        caut(x,j,k);
    else caut(j+1,y,k-t);
    return 0;
}
int main(void)
{
    srand(time(NULL));
    ifstream fin("sdo.in");
    ofstream fout("sdo.out");
    fin>>n>>k;
    for (i=1;i<=n;i++)
        fin>>v[i];
    caut(1,n,k);
    fout<<v[k]<<"\n";
    return 0;
}