标签:blog http io ar os sp for 数据 on
一天,Bessie 在眺望美丽的威斯康星的群山的时候,她产生了疑问:哪座山是最宽的? 她决定在地平线上,利用她的新大量程山峰高度测量仪依次做 N (1 <= N <= 10,000)次高度测量 H_i (1 <= H_i <= 1,000,000,000)。 一座山定义为一段连续的高度序列,序列中的高度一开始单调上升(或者不变),然后单调下降(或者不变)。举例来说,2, 3, 3, 5, 4, 4, 1 这一段高度序列就是一座山。如果在她的视线范围内有一段只单调上升或者只单调下降的序列,也算是一座山。注意,两座山之间可能有部分重叠。 山的宽度定义为在这个山上进行的测量的次数(也就是序列的长度)。例如序列 2, 3, 3,5, 4, 4, 1的宽度为 7.请帮 Bessie找到最宽的山。 这是一个比较典型的地平线的例子:
******* *
********* ***
********** *****
*********** ********* *
* ***************** *********** *** *
** ******************* ************* * * ******* *
**********************************************************************
?ddsssuussuussssssddddssddssssuuuuuuuuddddddssududssssuuudduddsssssuds
3211112333677777776543332111112344456765432111212111112343232111111211
aaaaa cccccccccccccccccccc eeeeeee ggggggggg
bbbbbbbbbbbbbbbbbbbbbbbbbbbb ddddd ffffffffff hhhhhhhhh
山标记为‘a‘, ‘b‘等等。显然,山 b有着最大的宽度,宽度为 28。
第 1 行: 一个单独的整数: N 第 2 到第N+1 行: 第i+1包含一个单独的整数: H_i
第 1 行: 一个单独的整数,表示最宽的山的宽度。
7 3 2 3 5 4 1 6
测量到的高度分别为 3, 2, 3, 5, 4, 1, 6.
5
在最宽的山处测量到的高度为 2, 3, 5, 4, 1. 其他的山包括 3, 2 和 1, 6。
数一哥哥教我的,萌萌哒,思想很巧妙
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<algorithm>
#include<cstdlib>
#include<queue>
#include<vector>
#include<set>
using namespace std;
int n,a[100010],ans,cnt;
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
ans=1;
for(int i=1;i<=n;i++)
{
cnt=0;
int j=i+1;
if(a[i]>a[i+1])
{
cnt++;
while(j<=n&&a[j-1]>=a[j])
cnt++,j++;
}
else
{
cnt++;
while(j<=n&&a[j-1]<=a[j])
cnt++,j++;
while(j<=n&&a[j-1]>=a[j])
cnt++,j++;
}
ans=max(ans,cnt);
}
printf("%d\n",ans);
return 0;
}
标签:blog http io ar os sp for 数据 on
原文地址:http://www.cnblogs.com/a972290869/p/4109564.html