Cod sursa(job #335119)

Utilizator 05_YohnE1 La5c01 05_Yohn Data 28 iulie 2009 18:48:50
Problema Secv Scor 50
Compilator fpc Status done
Runda Arhiva de probleme Marime 1.3 kb
var v,x:array[1..5000]of longint;
p1,u1:array[1..5000]of integer;
n,m,p,u,min,h,k,i,j:integer;
ok:boolean;

procedure sort(i,j:integer);
var a,b,aux,mij:longint;
begin
a:=i;
b:=j;
mij:=x[(i+j)div 2];
repeat
while x[a]<mij do inc(a);
while x[b]>mij do dec(b);
if a<=b then begin
   aux:=x[a]; x[a]:=x[b]; x[b]:=aux;
   inc(a);dec(b);
   end;
until a>b;
if i<b then sort(i,b);
if j>a then sort(a,j);
end;

begin
assign(input,'secv.in');reset(input);
assign(output,'secv.out');rewrite(output);
read(n);
m:=0;
for i:=1 to n do begin
    read(v[i]);
    ok:=false;
    for j:=1 to m do if v[i]=x[j] then ok:=true;
    if not ok then begin
              inc(m);
              x[m]:=v[i];
              end;
end;
sort(1,m);
p:=0;u:=0;
for i:=1 to n do begin
    if v[i]=x[1] then begin
                      inc(p);p1[p]:=i;
                      end;
    if v[i]=x[m] then begin
                      inc(u);u1[u]:=i;
                      end;
end;

min:=n+1;
for i:=1 to p do
    for j:=1 to u do
        if u1[j]-p1[i]+1>=m then begin
           h:=2;
           for k:=p1[i]+1 to u1[j]-1 do if v[k]=x[h] then inc(h);
           if (h=m)and(u1[j]-p1[i]+1<min) then min:=u1[j]-p1[i]+1;
        end;
if min=n+1 then writeln(-1)
           else writeln(min);
close(output);
end.