Cod sursa(job #509359)

Utilizator vendettaSalajan Razvan vendetta Data 10 decembrie 2010 22:19:51
Problema Subsir crescator maximal Scor 5
Compilator fpc Status done
Runda Arhiva educationala Marime 1.33 kb
const f='scmax.in';g='scmax.out';
    nmax=100001;
var
    i,j,poz,n,nr,maxim,k:longint;
    v,best,pp,l:array[0..nmax] of longint;

function caut (x:integer):integer;
    var
        p,u,m:integer;
    begin
        p:=0;
        u:=nr;
        m:=(p+u)div 2;
        while (p<=u) do
            begin
            if (v[l[m]]<x) and (v[l[m+1]]>=x) then caut:=m
            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;
        caut:=nr;
            end;
    end;
begin
    assign(input,f);reset(input);
    assign(output,g);rewrite(output);
    readln(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]);
        pp[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;
    write(maxim);
    writeln;
    //afisare(poz);
    close(input);close(output);
end.