Description
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:
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.
Input
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.
Output
Print a single number, the maximal number of watered sections if we create artificial rain above exactly one section.
Sample Input
1 2
1
5 1 2 1 2 1
3
8 1 2 1 1 1 3 3 4
6
题解:这题相当于是给几个不同高度的长方体并在一起,问在哪个长方体上方浇水,流过的长方体个数最大,输出最大值。知道水是从高向低流,枚举每个长方体相邻长方体比自身低或等高的就行,求出最大值。
#include <cstdio>
int main()
{
int n,a[1010];
while(~scanf("%d",&n))
{
for(int i=0;i<n;i++)
scanf("%d",&a[i]);
if(n==1)
{
printf("1\n");
continue;
}
else
{
int ans=1;
int max=0;
for(int i=0;i<n;i++)
{
if(i==0)
{
for(int j=1;j<n;j++)
{
if(a[j]<=a[j-1])
ans++;
<span style="white-space:pre"> </span>else
<span style="white-space:pre"> </span>break;
}
}
else if(i==n-1)
{
for(int j=n-2;j>=0;j--)
{
if(a[j]<=a[j+1])
ans++;
<span style="white-space:pre"> </span>else
<span style="white-space:pre"> </span>break;
}
}
else
{
for(int j=i-1;j>=0;j--)
{
if(a[j]<=a[j+1])
ans++;
else
break;
}
for(int j=i+1;j<n;j++)
{
if(a[j]<=a[j-1])
ans++;
else
break;
}
}
if(max<ans)
{
max=ans;
}
ans=1;
}
printf("%d\n",max);
}
}
return 0;
}