【USACO】山峰暸望
一天,Bessie在眺望美丽的威斯康星的群山的时候,她突然产生了疑问:哪座山是最宽的捏?她决定在地平线上,利用她的新发明的山峰高度测量仪依次做N (1 <= N <= 10,000)次高度测量H_i (1 <= H_i <= 1,000,000,000)。一座山定义为一段连续的高度序列,序列中的高度一开始单调上升(或者不变),然后单调下降(或者不变)。举例来说,2, 3, 3, 5, 4, 4, 1这一段高度序列就是一座山。如果在她的视线范围内有一段只单调上升或者只单调下降的序列,也算是一座山。山的宽度定义为在这个山上进行的测量的次数(也就是序列的长度)。请帮Bessie找到最宽的山。
这是一个比较典型的群山的例子:
******* *
********* ***
********** *****
*********** ********* *
* ***************** *********** *** *
** ******************* ************* * * ******* *
**********************************************************************
3211112333677777776543332111112344456765432111212111112343232111111211
aaaaaa ccccccccccccccccccccc eeeeeee ggggggggg
bbbbbbbbbbbbbbbbbbbbbbbbbbbb ddddd ffffffffff hhhhhhhhh
山标记为'a', 'b'等等。显然,山b有着最大的宽度,宽度为28。
第1行: 一个单独的整数: N
var f1,h,b,f2:array[0..10001]of longint;
n,ans,i,j,k,l,x,s:longint;
begin
readln(n);
for i:=1 to n do
readln(h[i]);
ans:=0;
x:=1;
b[1]:=0;
for i:=2 to n do
begin
if b[i-1]=0 then
begin
x:=x+1;
if h[i]>h[i-1] then b[i]:=1
else
if h[i]=h[i-1] then b[i]:=b[i-1]
else b[i]:=2
end
else
if b[i-1]=1 then
begin
x:=x+1;
if h[i]>h[i-1] then b[i]:=1
else
if h[i]=h[i-1] then b[i]:=b[i-1]
else b[i]:=2
end
else
begin
if h[i]>h[i-1] then
begin
if x>ans then ans:=x;
x:=2;
for j:=i-2 downto 1 do
if h[j]=h[i-1] then inc(x)
else break;
b[i]:=1
end
else
if h[i]<h[i-1] then
begin
b[i]:=2;
inc(x)
end
else
begin
b[i]:=b[i-1];
inc(x);
end;
end;
end;
if x>ans then ans:=x;
writeln(ans);
end.