Pagini recente » Rating Dobre Luca-Andrei (Luca2010) | Diferente pentru utilizator/eugenstoica intre reviziile 22 si 39 | Diferente pentru utilizator/eugenstoica intre reviziile 28 si 39 | cntnk | Cod sursa (job #5288)
Cod sursa(job #5288)
program secv;
var fi,g:text;
v,aux:array[1..5001] of longint;
b,f:array[1..5001] of longint;
k,n:longint;
procedure iofile;
var i,j:longint;
ok:boolean;
begin
k:=0;
assign(fi,'secv.in');
reset(fi);
assign(g,'secv.out');
rewrite(g);
readln(fi,n);
for i:=1 to n do
begin
read(fi,v[i]);
ok:=false;
for j:=1 to k do
if aux[j]=v[i] then
begin
ok:=true;
break;
end;
if not ok then
begin
inc(k);
aux[k]:=v[i];
end;
end;
close(fi);
end;
procedure prel;
var i,min,max,j:longint;
begin
max:=-maxlongint;
for i:=n downto 1 do
begin
b[i]:=1;
f[i]:=i;
for j:=i+1 to n do
if v[i]<v[j] then
if b[i]<b[j]+1 then
begin
b[i]:=b[j]+1;
f[i]:=f[j];
end;
if b[i]>max then max:=b[i];
end;
if max<>k then
begin
writeln(g,'-1');
close(g);
halt;
end;
min:=maxlongint;
for i:=1 to n do
if b[i]=max then
if f[i]-i<min then
min:=f[i]-i;
writeln(g,min+1);
close(g);
end;
begin
iofile;
prel;
end.