Cod sursa(job #1361345)

Utilizator trinc2014Trinc Adrian trinc2014 Data 25 februarie 2015 20:49:15
Problema Subsir crescator maximal Scor 100
Compilator fpc Status done
Runda Arhiva educationala Marime 0.93 kb
program subs_crec_maximal;
var f,g:text;
    n,i,x,nr,j:longint;
    b,t,tt:array[1..100000] of longint;

function max(a,b:longint):longint;
begin
   if a>b then max:=a
          else max:=b;
end;

function cauta(xx,st,dr:longint):longint;
var m:longint;
begin
   while st<=dr do
     begin
       m:=(st+dr) div 2;
       if xx<=b[m] then
         dr:=m-1
           else
         st:=m+1;
     end;
   cauta:=st;
end;

begin
  assign(f,'scmax.in'); reset(f);
  assign(g,'scmax.out'); rewrite(g);
  readln(f,n);
  for i:=1 to n do
    read(f,t[i]);
  b[1]:=t[1];
  tt[1]:=1; x:=1;
  for i:=2 to n do
    begin
      j:=cauta(t[i],1,x);
      b[j]:=t[i];
      tt[i]:=j;
      x:=max(j,x);
    end;

  nr:=x;
  for i:=n downto 1 do
    if tt[i]=nr then
      begin
        b[nr]:=t[i];
        dec(nr);
      end;
  writeln(g,x);
  for i:=1 to  x do
    write(g,b[i],' ');
  close(f); close(g);
end.