Cod sursa(job #2401672)

Utilizator mihaitamoglanmihai moglan mihaitamoglan Data 9 aprilie 2019 21:55:41
Problema Subsir crescator maximal Scor 90
Compilator fpc Status done
Runda Arhiva educationala Marime 1.26 kb
type tablou=array[0..100004]of longint;
var best,t,p,l:tablou;
    i,j,k,m,n,maxim,nr,poz:longint;
    f,g:text;

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


function caut(x:longint):longint;
var p,u,m:longint;
ok:boolean;
begin
 p:=0;
 u:=nr;
 m:=(p+u)div 2;
 ok:=true;
 while (p<=u)and ok do
  begin
    if(t[l[m]]<x)and(t[l[m+1]]>=x) then begin
                                   caut:=m;
                                   ok:=false;
                                   end
    else if(t[l[m+1]]<x) then
                        begin
                          p:=m+1;
                          m:=(p+u) div 2;
                        end
    else begin
      u:=m-1;
      m:=(u+p) div 2;
      end;
  end;
 if ok then caut:=nr;

end;

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