Pagini recente » Cod sursa (job #895982) | Cod sursa (job #1709048) | Cod sursa (job #1298208) | Cod sursa (job #214524) | Cod sursa (job #778664)
Cod sursa(job #778664)
var a,b:array[0..5001]of longint; i,j,n,k,k1,j2,s,k2:integer; t,t2:boolean;
procedure quicksort (ins,sf:integer);
var i,s:integer; pivot,k:longint;
begin
i:=ins; s:=sf; pivot:=a[(i+s) div 2];
repeat
while pivot>a[i] do inc(i);
while pivot<a[s] do dec(s);
if i<=s then begin k:=a[i]; a[i]:=a[s]; a[s]:=k; inc(i); dec(s) end;
until i>s;
if (s>ins) then quicksort(ins,s);
if (i<sf)then quicksort(i,sf);
end;
begin
assign(input,'secv.in'); reset(input);
read(n);
for i:=1 to n do begin read(a[i]); b[i]:=a[i];end;
quicksort(1,n);j:=1;k:=0; for i:=1 to n do if b[i]=a[1] then if k=0 then k:=i; s:=5001;
assign(output,'secv.out'); rewrite(output); a[n+1]:=-1;
while a[j]=a[1] do
begin
t:=true;
k1:=k; k2:=k; k:=0;
if k1<>0 then
for i:=j+1 to n do
if a[i]<>a[i-1] then
begin
j2:=k2;
while (b[j2]<>a[i]) and (j2<n) do begin inc(j2); if b[j2]=a[1] then if k=0 then k:=j2 end;
if b[j2]=a[i] then k2:=j2 else t:=false;
end;
if k=0 then begin k:=k2; while (k<n) and (b[k]<>a[1]) do inc(k); end;
if t then begin
if s>k2-k1 then begin s:=k2-k1; t2:=true; end; end;
inc(j);
end;
if t2 then begin if n=0 then writeln('0') else writeln(s+1) end else writeln('-1');
close(output);
end.