Cod sursa(job #1350581)

Utilizator mariusadamMarius Adam mariusadam Data 20 februarie 2015 20:46:42
Problema Subsir crescator maximal Scor 100
Compilator fpc Status done
Runda Arhiva educationala Marime 1.02 kb
{Subsir crescator de lungima maxima.Complexitate O(n*log2N)}
program scmax;
type tabel=array[0..100001] of longint;
     buf=array[0..100001] of longint;
var ff1,ff2:buf;
    t,b,tt:tabel;
    i,j,nr,x,n:longint;
    f1,f2:text;

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 (f1,'scmax.in');
 assign (f2,'scmax.out');
 reset (f1);
 rewrite (f2);
 settextbuf(f1,ff1);
 settextbuf(f2,ff2);
 readln (f1,n);
 for i:=1 to n do
  read (f1,t[i]);
 b[1]:=t[1]; x:=1; tt[1]:=1;
 for i:=2 to n do
  begin
   j:=cauta(t[i],1,x);
   b[j]:=t[i]; tt[i]:=j;
   x:=max(x,j);
  end;
 nr:=x;
 for i:=n downto 1 do
 if tt[i]=nr then
  begin
   b[nr]:=t[i]; nr:=nr-1;
  end;
 writeln (f2,x);
 for i:=1 to x do
  write (f2,b[i],' ');
 close (f1);
 close (f2);
end.