D. Remove One Element-----思维/dp

You are given an array a consisting of n integers.

You can remove at most one element from this array. Thus, the final length of the array is n−1 or n.

Your task is to calculate the maximum possible length of the strictly increasing contiguous subarray of the remaining array.

Recall that the contiguous subarray a with indices from l to r is a[l…r]=al,al+1,…,ar. The subarray a[l…r] is called strictly increasing if al<al+1<⋯<ar.

Input
The first line of the input contains one integer n (2≤n≤2⋅105) — the number of elements in a.

The second line of the input contains n integers a1,a2,…,an (1≤ai≤109), where ai is the i-th element of a.

Output
Print one integer — the maximum possible length of the strictly increasing contiguous subarray of the array a after removing at most one element.

Examples
inputCopy

5
1 2 5 3 4
outputCopy
4
inputCopy
2
1 2
outputCopy
2
inputCopy
7
6 5 4 3 2 4 3
outputCopy
2
Note
In the first example, you can delete a3=5. Then the resulting array will be equal to [1,2,3,4] and the length of its largest increasing subarray will be equal to 4.

题意:给你n个数,最多只能去掉一个,构成一个[l,r]满足al<al+1<al+2<ar使得长度最大。

解析:遍历每个i,统计i之前的递增的个数,和i之后递增后的个数。
统计最大两种情况
1 不去掉i
2 去掉i,去掉就是要满足a[i-1]<a[i+1] .就是(i-1)递增的个数,(i+1)递增的个数


#include<bits/stdc++.h>
using namespace std;
const int N=1e6+1000;
int f[2][N];
int a[N];
int n;
int main()
{
	cin>>n;
	for(int i=1;i<=n;i++) cin>>a[i];
	for(int i=1;i<=n;i++)
	{
		if(a[i]>a[i-1]) f[0][i]=f[0][i-1]+1;
		else f[0][i]=1;
	}
	for(int i=n;i>=0;i--)
	{
		if(a[i+1]>a[i]) f[1][i]=f[1][i+1]+1;
		else f[1][i]=1;
	}
	int ans=0;
	for(int i=1;i<=n;i++)
	{
		ans=max(ans,f[1][i]);
		if(a[i-1]<a[i+1])
		{
			ans=max(ans,f[0][i-1]+f[1][i+1]);
		}
	}
	cout<<ans<<endl;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值