Cod sursa(job #696810)

Utilizator boby301Bogdan Bacila boby301 Data 28 februarie 2012 20:14:11
Problema Statistici de ordine Scor 30
Compilator fpc Status done
Runda Arhiva educationala Marime 0.92 kb
var n,k:int64;
    v:array[1..3000000]of int64;
    i:longint;
    f,g:text;
    buf1,buf2:array[1..1 shl 17]of char;
procedure swap(var aa:int64;var bb:int64);
var aux:int64;
begin
  aux:=aa;
  aa:=bb;
  bb:=aux;
end;

procedure quicksort(st, dr:int64);
var 
  i, j, pivot,a:int64;
begin
  i := st; j := dr;a:=random(dr-st)+st;
  pivot:= v[a];
  repeat
    while (i < dr) and (v[i] < pivot) do inc(i);
    while (j > st) and (pivot < v[j]) do dec(j);
    if i <= j then 
    begin
      if i < j then swap(v[i],v[j]);
      if i < dr then inc(i);
      if j > st then dec(j);
    end
  until i > j;
  if k<j then quicksort(st, j)
         else if k>i then quicksort(i, dr);
end;

begin
assign(f,'sdo.in');settextbuf(f,buf1);reset(f);
assign(g,'sdo.out');settextbuf(g,buf2);rewrite(g);
randomize;
readln(f,n,k);
for i:=1 to n do 
  read(f,v[i]);

quicksort(1,n);

write(g,v[k]);

{for i:=1 to n do
  write(g,v[i],' ');}
close(f);close(g);   
end.