Cod sursa(job #361600)

Utilizator philipPhilip philip Data 5 noiembrie 2009 23:47:54
Problema Subsir crescator maximal Scor 90
Compilator fpc Status done
Runda Arhiva educationala Marime 0.97 kb
var a,best,prev:array[0..100005] of longint;
    j,i,n,max:longint;

function caut(st,dr:longint):longint;
  var mij:longint;
  begin
    if st=dr then
      if a[best[st]]<a[i] then caut:=st
        else caut:=st-1
    else begin
      mij:=(st+dr) shr 1;
      if a[best[mij]]>a[i] then caut:=caut(st,mij)
        else caut:=caut(mij+1,dr);
    end;
  end;

procedure afiseaza(i:longint);
  begin
    if prev[i]<>0 then afiseaza(prev[i]);
    write(a[i],' ');
  end;

begin
  assign(input,'scmax.in');
  reset(input);
  assign(output,'scmax.out');
  rewrite(output);
  readln(n);
  for i:=1 to n do read(a[i]);
  best[1]:=1;
  max:=1;
  for i:=2 to n do begin
        j:=caut(1,max);
        prev[i]:=best[j];
        if (j=max) or (a[i]<a[best[j+1]]) then begin
                best[j+1]:=i;
                if max<j+1 then max:=j+1;
        end;
  end;

  writeln(max);
  i:=best[max];
  afiseaza(i);
  close(input);
  close(output);
end.