Pagini recente » Cod sursa (job #696233) | Cod sursa (job #2035062) | Cod sursa (job #2126522) | Cod sursa (job #268948) | Cod sursa (job #66264)
Cod sursa(job #66264)
const MaxN=5002;
type vektor=array[1..MaxN] of longint;
var
fin,fout:text;
a,c:vektor;
n,i,j,m,z:longint;
min,minh:longint;
ok:boolean;
procedure QSort(var a:vektor;Lo, Hi: longint);
procedure Sort(l, r:longint);
var
i, j, x, y: longint;
begin
i := l; j := r; x := a[random(r-l)+l];
repeat
while a[i] < x do i := i + 1;
while x < a[j] do j := j - 1;
if i <= j then
begin
y := a[i]; a[i] := a[j]; a[j] := y;
i := i + 1; j := j - 1;
end;
until i > j;
if l < j then Sort(l, j);
if i < r then Sort(i, r);
end;
begin
Sort(Lo,Hi);
end;
begin
assign(fin,'secv.in');reset(fin);
assign(fout,'secv.out');rewrite(fout);
readln(fin,n);
if n=0 then writeln(fout,0)
else if n=1 then writeln(fout,1)
else begin
min:=maxlongint;
m:=0;
for i:=1 to n do begin
read(fin,a[i]);
ok:=true;
if min>a[i] then min:=a[i];
for j:=1 to m do
if a[i]=c[j] then ok:=false;
if ok then begin
m:=m+1;
c[m]:=a[i];
end;
end;
qsort(c,1,m);
minh:=maxlongint;
for i:=1 to n-m+2 do begin
if min=a[i] then begin
z:=i+1;
j:=2;
while (z<=n)and(j<=m) do begin
if a[z]=c[j] then j:=j+1
else z:=z+1;
end;
if (j=m+1)and(z<=n) then begin
if minh>z-i+1 then minh:=z-i+1;
end;
end;
end;
if minh=maxlongint then writeln(fout,-1)
else writeln(fout,minh);
end;
close(fout);
end.