Cod sursa(job #307678)

Utilizator Simona14Stanescu Simona Simona14 Data 24 aprilie 2009 17:46:00
Problema Secv Scor 100
Compilator fpc Status done
Runda Arhiva de probleme Marime 0.83 kb
var n,m,rez,i,j,k,x:longint;
v,t,c:array[1..5000] of longint;
begin
assign(input,'secv.in');
reset(input);
readln(n);
rez:=1000000000;
for i:= 1 to n do begin
read(v[i]); t[i]:=v[i]; end;
for i:= 1 to n do
for j:=i+1 to n do
if t[i]>t[j] then begin
                  x:=t[i];
                  t[i]:=t[j];
                  t[j]:=x;
                  end;
c[1]:=t[1];
m:=1;
for i:= 1 to n do
if t[i]<>t[i-1] then begin
                     inc(m);
                     c[m]:=t[i];
                     end;
for i:= n downto 1 do begin
if v[i]=c[m] then begin
k:=i;
for j:=m-1 downto 1 do begin
while (k>0) and (v[k]<>c[j]) do dec(k);
if (k<=0) then break;
end;
if (k>0) and (rez>i-k+1) then rez:=i-k+1;
end;
end;
assign(output,'secv.out'); rewrite(output);
if rez=1000000000 then writeln(-1) else writeln(rez);
end.