Little Petya often travels to his grandmother in the countryside. The grandmother has a large garden, which can be represented as a rectangle 1 × n in size, when viewed from above. This rectangle is divided into n equal square sections. The garden is very unusual as each of the square sections possesses its own fixed height and due to the newest irrigation system we can create artificial rain above each section.
Creating artificial rain is an expensive operation. That's why we limit ourselves to creating the artificial rain only above one section. At that, the water from each watered section will flow into its neighbouring sections if their height does not exceed the height of the section. That is, for example, the garden can be represented by a 1 × 5 rectangle, where the section heights are equal to 4, 2, 3, 3, 2. Then if we create an artificial rain over any of the sections with the height of 3, the water will flow over all the sections, except the ones with the height of 4. See the illustration of this example at the picture:
![](https://i-blog.csdnimg.cn/blog_migrate/4bba96a6b0ed33edb17a77da428800dc.png)
As Petya is keen on programming, he decided to find such a section that if we create artificial rain above it, the number of watered sections will be maximal. Help him.
The first line contains a positive integer n (1 ≤ n ≤ 1000). The second line contains n positive integers which are the height of the sections. All the numbers are no less than 1 and not more than 1000.
Print a single number, the maximal number of watered sections if we create artificial rain above exactly one section.
1 2
1
5 1 2 1 2 1
3
8 1 2 1 1 1 3 3 4
6
题意:简化下意思,就是给你若干个紧挨着的长方形,这些长方形的宽度为1,高度为你所输入的值,比如图上的三号长方形,它的高度为3,如果在它上面浇水,水最多能漫多远距离(当低的长方形遇到比它高的长方形时,就停止流动)。这个题目就是求这些长方形中,漫水最远的那个长方形所漫的最远长度。
解题思路:看着题目数据不大,一个一个长方形求(枚举)。以每个长方形为起点,向左右两边扫,每扫完一个起点,则更新下max。
#include <stdio.h> int a[1010]; int main() { int n; while(scanf("%d",&n)!=EOF) { for(int i=0;i<n;i++) { scanf("%d",&a[i]); } int max=-1;//初始化max for(int i=0;i<n;i++)//大循环控制起点 { int cnt=1;//包括这个起点本身,所以初始化为1 int tmp=i;//tmp作每次的小起点 for(int j=tmp-1;j>=0;j--)//向左扫 { if(a[j]>a[tmp])//遇到高的了 break; tmp--; cnt++; } tmp=i; for(int j=tmp+1;j<n;j++)//向右扫 { if(a[j]>a[tmp])//遇到高的了 break; tmp++; cnt++; } if(cnt>max)//更新max { max=cnt; } } printf("%d\n",max); } return 0; }