Cod sursa(job #509368)

Utilizator vendettaSalajan Razvan vendetta Data 10 decembrie 2010 22:38:00
Problema Subsir crescator maximal Scor 100
Compilator fpc Status done
Runda Arhiva educationala Marime 2.01 kb
const f='scmax.in';g='scmax.out';
    nmax=100001;
var
    x,p,z,q,i,n,j:longint;
    v,b,c,a:array[0..nmax] of longint;
    gasit:boolean;

function cautb (n,x:longint):longint;
    var
        st,dr,mij:longint;
        ok:boolean;
    begin
        st:=1;
        dr:=n;
        ok:=false;
        while (ok=false) and (st<=dr)do
            begin
            mij:=(st+dr) div 2;
            if st=dr then ok:=true
            else
                if v[mij]=x then
                    begin
                    dr:=mij;
                    break;
                    end
                        else
                if (v[mij]<x) and (x<v[mij]+1) then
                    begin
                    dr:=mij+1;
                    break;
                    end
                        else
                    if x<v[mij] then dr:=mij else
                    if x>v[mij] then st:=mij+1 else
                    end;
        cautb:=dr;
    end;
begin
    assign(input,f);reset(input);
    assign(output,g);rewrite(output);
    readln(n);
    read(x);
    q:=1;a[1]:=x;
    v[q]:=x;
    b[1]:=1;
    gasit:=true;
    for i:=2 to n do
        begin
        read(x);a[i]:=x;
        if a[i]<>a[i-1] then gasit:=false;
        if x>v[q] then
            begin
            q:=q+1;p:=i;
            b[i]:=q;
            v[q]:=x;
            end
        else
            begin
            j:=cautb(q,x);
            v[j]:=x;
            b[i]:=j;
            end;
        end;
    writeln(q);
    z:=q;
    if q=1 then write(a[1]) else
    if gasit then write(a[1])
        else
        begin
        while (q>0) do
            begin
            c[q]:=a[p];
            q:=q-1;
            for i:=p-1 downto 1 do
                if(b[i]=q) and (a[i]<c[q+1]) then
                    begin
                    p:=i;
                    break
                    end;
            end;
        for i:=1 to z do write(c[i],' ');
        end;
    close(input);close(output);
end.