Cod sursa(job #1091473)

Utilizator mariusadamMarius Adam mariusadam Data 25 ianuarie 2014 18:45:17
Problema Subsir crescator maximal Scor 100
Compilator fpc Status done
Runda Arhiva educationala Marime 1.24 kb
{Subsir crescator maximal O(N*log2N)}
program scmax;
var v,best,p,L:array[0..100001] of longint;
    n,k,max,nr,i,j,poz:longint;
    f,g:text;

procedure afis(i:longint);
begin
 if p[i]>0 then
  afis(p[i]);
 write(g,v[i],' ');
end;

function caut(x:longint):longint;
var p,u,m:longint;
begin
 p:=0; u:=nr; m:=(p+u) div 2;
 while p<=u do
  begin
   if m=nr then
    begin
     if v[L[m]]<x then
      begin
       caut:=m;
       p:=u+1;
      end;
     end
    else
   if (v[L[m]]<x) and (v[L[m+1]]>=x) then
    begin
     caut:=m;
     p:=u+1;
    end
   else
   if v[L[m+1]]<x then
    begin
     p:=m+1;
     m:=(p+u) div 2;
    end
   else
    begin
     u:=m-1;
     m:=(p+u) div 2;
    end;
  end;
 //caut:=nr;
end;

begin
 assign(f,'scmax.in'); reset(f);
 assign(g,'scmax.out'); rewrite(g);
 readln(f,n);
 nr:=1;
 for i:=1 to n do
  read(f,v[i]);
 best[1]:=1; L[1]:=1; L[0]:=0;
 for i:=2 to n do
  begin
   poz:=caut(v[i]);
   p[i]:=L[poz];
   best[i]:=poz+1;
   L[poz+1]:=i;
   if nr<poz+1 then
    nr:=poz+1;
  end;
 max:=0; poz:=0;
 for i:=1 to n do
  if max<best[i] then
   begin
    max:=best[i];
    poz:=i;
   end;
 writeln(g,nr);
 afis(L[nr]);
 close(f);
 close(g);
end.