Cod sursa(job #1744355)

Utilizator Dan_RadulescuRadulescu Dan Dan_Radulescu Data 19 august 2016 17:23:59
Problema Statistici de ordine Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.85 kb
#include<fstream>
#include<stdlib.h>
using namespace std;
ifstream fin("sdo.in");
ofstream fout("sdo.out");
int n,v[3000002],i,j,k;
int pos(int st,int dr){
    int i,j,val,aux;
    val=v[st+rand()%(dr-st+1)];
    i=st-1;j=dr;
    while(0<1){
        while (v[i]<val) i++;
        while (v[j]>val) j--;
        if (i<j){
            aux=v[i];
            v[i]=v[j];
            v[j]=aux;
        }
          else
            return j;
    }
}
int sord(int st,int dr,int poz){
    int q,pivot;
    if (st==dr) return v[st];
    pivot=pos(st,dr);
    q=pivot-st+1;
    if (q==poz) return v[pivot];
    if (q>poz) return sord(st,q-1,poz);
      else
        return sord(q+1,dr,poz-q);
}
int main(){
    fin>>n>>k;
    for (i=1;i<=n;i++)
        fin>>v[i];
    fin.close();
    fout<<sord(1,n,k);
    fout.close();
    return 0;
}