Cod sursa(job #697205)

Utilizator boby301Bogdan Bacila boby301 Data 28 februarie 2012 23:05:05
Problema Statistici de ordine Scor 0
Compilator fpc Status done
Runda Arhiva educationala Marime 0.98 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) and (j>st) then quicksort(st,j)
         else if (k>=i) and (i<dr) 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);

writeln(v[k]);

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