Cod sursa(job #34598)

Utilizator ProtomanAndrei Purice Protoman Data 20 martie 2007 22:03:40
Problema Secv Scor 60
Compilator fpc Status done
Runda Arhiva de probleme Marime 1.06 kb
var f1,f2:text; a,v:array[1..5000] of integer; i,j,ad,n,max,min,ult,h,t,m,g,ind:longint;
begin
        assign(f1,'secv.in');
        reset(f1);
        assign(f2,'secv.out');
        rewrite(f2);
        read(f1,n);
        min:=maxlongint;
        max:=-maxlongint;
        t:=min;
        for i:=1 to n do begin
                read(f1,a[i]);
                if a[i]<min then begin min:=a[i]; j:=0; end;
                if a[i]=min then begin inc(j); v[j]:=i; end;
                if max<a[i] then max:=a[i];
                if max=a[i] then ult:=i;
        end;
        m:=j;
        for j:=1 to m do begin
                h:=max-min;
                ind:=min+1;
                ad:=0;
                for i:=v[j] to ult do begin
                        if a[i]=ind then inc(ind);
                        if ind=max+1 then begin g:=i; ad:=1; break; end;
                end;
                if (g-v[j]+1<t)and(ad=1) then t:=g-v[j]+1;
        end;
        if t=maxlongint then t:=-1;
        write(f2,t);
        close(f1);
        close(f2);
end.