Cod sursa(job #269732)

Utilizator philipPhilip philip Data 3 martie 2009 12:20:05
Problema Subsir crescator maximal Scor 10
Compilator fpc Status done
Runda Arhiva educationala Marime 1.02 kb
var f,g:text;
    i,n,j,max,m:longint;
    st,dr,t,r,a:array[0..100000] of longint;
    ok:boolean;

procedure citire;
  begin
    assign(f,'scmax.in');
    reset(f);
    readln(f,n);
    read(f,a[1]);
    j:=1;
    for i:=2 to n do begin
      read(f,a[i]);
      j:=1;
      ok:=true;
      while ok do begin
        if a[i]>a[j] then if dr[j]<>0 then j:=dr[j] else begin
          dr[j]:=i;
          ok:=false;
          r[i]:=r[j]+1;
          if r[i]>max then begin
            max:=r[i];
            m:=i;
          end;
          t[i]:=j;
        end
          else if a[i]<a[j] then if st[j]<>0 then j:=st[j] else begin
            st[j]:=i;
            ok:=false;
            r[i]:=r[j]+1;
         {   t[i]:=j; }
          end else ok:=false;


      end;

    end;
  end;

procedure afisare(m:longint);
  begin
    if t[m]<>0 then afisare(t[m]);
    write(g,a[m],' ');
  end;

begin
  citire;
  assign(g,'scmax.out');
  rewrite(g);
  writeln(g,max+1);
  afisare(m);
  close(g);
end.