Cod sursa(job #1101656)

Utilizator Mihai_ChihaiMihai Chihai Mihai_Chihai Data 8 februarie 2014 21:03:42
Problema Statistici de ordine Scor 30
Compilator fpc Status done
Runda Arhiva educationala Marime 0.84 kb
program statistici_de_ordine;
var a:array[1..3000005] of longint;
    b1,b2:array[1..1 shl 16] of char;
    n,i,k,nr:longint;
procedure sort(l,r:longint);
var i,j,mid,aux:longint;
begin
i:=l;
j:=r;
mid:=(i+j) div 2;
while i<=j do begin
            while a[i]<a[mid] do inc(i);
            while a[j]>a[mid] do dec(j);
            if i<=j then begin
                 aux:=a[i];
                 a[i]:=a[j];
                 a[j]:=aux;
                 inc(i);
                 dec(j);
                 end;
            end;
if k>=i then if i<r then sort(i,r);
if k<=j then if l<j then sort(l,j);
end;

begin
assign(input,'sdo.in'); reset(input);
settextbuf(input,b1);
assign(output,'sdo.out'); rewrite(output);
settextbuf(output,b2);

readln(n,k);
for i:=1 to n do read(a[i]);
sort(1,n);
writeln(a[k]);
close(output);
end.