Cod sursa(job #482580)

Utilizator 05_YohnE1 La5c01 05_Yohn Data 3 septembrie 2010 21:27:52
Problema Subsir crescator maximal Scor 100
Compilator fpc Status done
Runda Arhiva educationala Marime 1.43 kb
program scmax;
var n,i,max,poz,nr:longint;
v,l,best,tata:array[0..100000]of longint;

procedure afis(x:longint);
begin
if tata[x]>0 then afis(tata[x]);
write(v[x],' ');
end;

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

begin
assign(input,'scmax.in');
reset(input);
read(n);
for i:=1 to n do read(v[i]);
nr:=1;
best[1]:=1;
l[1]:=1; l[0]:=0;
for i:=2 to n do begin
    poz:=caut(v[i]);
    tata[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;
assign(output,'scmax.out');
rewrite(output);
writeln(max);
afis(poz);
close(output);
end.