Pagini recente » Cod sursa (job #834245) | Cod sursa (job #1458610) | Cod sursa (job #1893759) | Cod sursa (job #868860) | Cod sursa (job #598827)
Cod sursa(job #598827)
var a,b:array[1..5000] of longint;
ok:boolean;
i,k,p,j,n,aux,min:longint;
begin
assign(input,'secv.in');reset(input);
assign(output,'secv.out');rewrite(output);
readln(n);
for i:=1 to n do read(a[i]);
b:=a;
repeat
ok:=true;
for i:=1 to n-1 do
if b[i]>b[i+1] then
begin
aux:=b[i];
b[i]:=b[i+1];
b[i+1]:=aux;
ok:=false;
end;
until ok;
k:=1;
for i:=2 to n do
if b[i]<>b[k] then
begin
inc(k);
b[k]:=b[i];
end;
min:=maxlongint;
for i:=1 to n do
if a[i]=b[1] then
begin
p:=2;j:=i+1;
while (p<=k) and (j<=n) do
begin
if a[j]=b[p] then inc(p);
inc(j);
end;
if (p=k+1) and (j-i<min) then min:=j-i;
end;
if min<>maxlongint then writeln(min) else writeln(-1);
end.