Cod sursa(job #553111)

Utilizator andreifirstCioara Andrei Ioan andreifirst Data 13 martie 2011 17:03:53
Problema Subsir crescator maximal Scor 10
Compilator fpc Status done
Runda Arhiva educationala Marime 0.82 kb
var v:array[1..100000] of longint;
    l, rec:array[1..100000] of longint;
    max, pmax, i, j, n:longint;
    buf1, buf2:array [1..100000] of char;
    f, g:text;

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

for i := n-1 downto 1 do
  begin
  max:=0; pmax:=0;
  for j := i+1 to n do
    begin
    if (v[j]>v[i]) and (l[j]>=max) then
      begin
      max:=l[j];
      pmax:=j;
      end;
    end;
  l[i]:=max+1;
  rec[i]:=pmax;
  end;

max:=0; pmax:=0;
for i := 1 to n do
  begin
  if l[i]> max then begin max:= l[i]; pmax:=i; end;
  end;

writeln (g, max+1);
while pmax <> 0 do
  begin
  write (g, v[pmax], ' ');
  pmax:=rec[pmax];;
  end;

close (f); close (g);
end.