首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >【CodeForces】66B - Petya and Countryside(递增子串变形题,模拟)

【CodeForces】66B - Petya and Countryside(递增子串变形题,模拟)

作者头像
FishWang
发布2025-08-26 15:52:35
发布2025-08-26 15:52:35
8600
代码可运行
举报
运行总次数:0
代码可运行

B. Petya and Countryside

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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.

Examples

input

代码语言:javascript
代码运行次数:0
运行
复制
1
2

output

代码语言:javascript
代码运行次数:0
运行
复制
1

input

代码语言:javascript
代码运行次数:0
运行
复制
5
1 2 1 2 1

output

代码语言:javascript
代码运行次数:0
运行
复制
3

input

代码语言:javascript
代码运行次数:0
运行
复制
8
1 2 1 1 1 3 3 4

output

代码语言:javascript
代码运行次数:0
运行
复制
6

用的和前一段做的严格递增递增子串的题差不多,这次也是把每一个递增的子串保存到结构体中。这个方法挺好用的,复杂度也不算太高。和数据量比起来松松AC。

代码如下:

代码语言:javascript
代码运行次数:0
运行
复制
#include <cstdio>
#include <algorithm>
using namespace std;
struct node
{
	int from,to;
	int l;
}data[1011];
int main()
{
	int n;
	int a[1011];
	int num;
	int ans;
	while (~scanf ("%d",&n))
	{
		for (int i = 1 ; i <= n ; i++)
			scanf ("%d",&a[i]);
		num = 1;
		data[1].from = 1;
		data[1].to = 1;
		data[1].l = 1;
		ans = 1;
		for (int i = 2 ; i <= n ; i++)
		{
			if (a[i] >= a[i-1])
			{
				data[num].to = i;
				data[num].l++;
				ans = max (data[num].l , ans);
			}
			else
			{
				num++;
				data[num].from = i;
				data[num].to = i;
				data[num].l = 1;
			}
		}
		for (int i = 1 ; i <= num ; i++)
		{
			int t = data[i].l;
			for (int j = data[i].to + 1 ; j <= n ; j++)
			{
				if (a[j] <= a[j-1])
					t++;
				else
					break;
			}
			ans = max (ans , t);
		}
		printf ("%d\n",ans);
	}
	return 0;
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2025-08-26,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档