Pagini recente » Cod sursa (job #2246237) | Cod sursa (job #2771247) | Cod sursa (job #3266783) | Cod sursa (job #3147931) | Cod sursa (job #31257)
Cod sursa(job #31257)
type vector=array[0..10000]of longint;
var v,x:vector;
i,j,n,nx,ls,ld,min:longint;
begin
assign(input,'secv.in');reset(input);
assign(output,'secv.out');rewrite(output);
readln(n);
read(v[1]);x[1]:=v[1]; nx:=1;
//construiesc vectorul de elemente distincte in ordine crescatoare
for i:=2 to n do begin
read(v[i]);
j:=1;while (x[j]<v[i])and(j<=nx) do inc(j);
if (v[i]<x[j])or(j>nx) then begin
for ls:=nx downto j do x[ls+1]:=x[ls];
x[j]:=v[i];
inc(nx);
end;
end;
//caut secventele ce contin subsir crescator pe x
min:=MaxLongInt;
for i:=1 to n do
if v[i]=x[1] then begin
ls:=i; ld:=i;
for j:=2 to nx do begin
while (v[ld]<>x[j])and(ld<=n) do inc(ld);
if ld>n then break;
end;
if (ld<=n)and(min>ld-ls+1) then min:=ld-ls+1;
end;
if min>n then write(-1) else write(min);
close(input);close(output);
end.