Cod sursa(job #298957)

Utilizator frozen62iceBLue FirE frozen62ice Data 6 aprilie 2009 15:06:28
Problema Subsir crescator maximal Scor 10
Compilator fpc Status done
Runda Arhiva educationala Marime 0.93 kb
var f,g:text;
    a,c,poz,d:array[0..100001] of longint;
    i,j,max,k,p,n,w:longint;
procedure insert(x,st,dr:longint);
 var m:longint;
 begin
  m:=(st+dr) div 2;
  if st>dr then begin
   c[st]:=x;
   inc(p);
   poz[p]:=st;
  end
   else
  if st=dr then begin
   if dr=k+1 then begin
    inc(k);
    c[k]:=x;
    inc(p);
    poz[p]:=k;
   end
    else begin
     inc(p);
     poz[p]:=st+1;
     c[st+1]:=x;
    end;
  end
   else
    if c[m]<x then insert(x,m+1,dr)
     else
    if c[m]>x then insert(x,st,m-1);
 end;
begin
assign(f,'scmax.in');reset(f);
assign(g,'scmax.out');rewrite(g);
read(f,n);
for i:=1 to n do begin
 read(f,a[i]);
 insert(a[i],1,k+1);
 if poz[p]>max then max:=poz[p];
end;
writeln(g,max);
for i:=p downto 1 do begin
 if poz[i]=max then begin
  inc(w);
  d[w]:=a[i];
  dec(max);
 end;
 if max<=0 then break;
end;
for i:=w downto 1 do write(g,d[i],' ');
close(f);
close(g);
end.