Cod sursa(job #51362)
Utilizator | Data | 11 aprilie 2007 15:40:36 | |
---|---|---|---|
Problema | Secv | Scor | 30 |
Compilator | fpc | Status | done |
Runda | Arhiva de probleme | Marime | 1.76 kb |
var v,smax:array[1..5000] of longint;
i1,j1,i,j,n,max:integer;
ok:boolean;
f:text;
begin
assign(f,'secv.in');reset(f);readln(f,n);
for i:=1 to n do read(f,v[i]);
assign(f,'secv.out');rewrite(f);
smax[n]:=1;
for i:=n-1 downto 1 do
begin
smax[i]:=0;
for j:=i+1 to n do
if (v[i]<v[j]) and (smax[j]>smax[i]) then smax[i]:=smax[j];
smax[i]:=smax[i]+1;
end;
max:=-1;
for i:=1 to n do
if smax[i]>=max then begin
max:=smax[i];
i1:=i;
end;
for i:=i1+1 to n do
if (v[i]>v[i1]) and (smax[i]=max-1) then begin
j1:=i;
max:=max-1;
end;
for i:=1 to i1-1 do
begin
ok:=true;
for j:=i1 to j1 do
if v[i]=v[j] then begin ok:=false;
break;
end;
if ok=true then begin
write(f,'-1');
close(f);
halt;
end;
end;
for i:=j1+1 to n do
begin
ok:=true;
for j:=i1 to j1 do
if v[i]=v[j] then begin ok:=false;
break;
end;
if ok=true then begin
write(f,'-1');
close(f);
halt;
end;
end;
write(f,j1-i1+1);
close(f);
end.