#605 (Div. 3)D.Remove One Elemen

题目描述

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

Input
5
1 2 5 3 4
Output
4
Input
2
1 2
Output
2
Input
7
6 5 4 3 2 4 3
Output
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.

题目大意

给你一个数组a[],你可以删除至多一个数。要求找出最长的单调上升的子序列的长度。

题目分析

我们可以分别从求出a[]左到右和从右到左单调递增序列的长度。
如:1 2 5 3 4
f[ ]:1 2 3 1 2
g[]:3 2 1 2 1
然后枚举所有删除的点i,如果a[i-1]<a[i+1],那么算出该序列长度f[i-1]+g[i+1]。
根据题目,我们可以删除点,也可以不删,因此还要枚举不删点的长度f[i]或者g[i],最后求出最大值即可。

代码如下
#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <stack>
#include <map>
#include <queue>
#include <vector>
#include <set>
#include <algorithm>
#include <iomanip>
#define LL long long
using namespace std;
const int N=2e5+5;
int a[N];
int f[N],g[N];
int main()
{
	int n;
	cin>>n;
	for(int i=1;i<=n;i++)
		cin>>a[i];
	
	for(int i=1;i<=n;i++)
		f[i]=g[i]=1;
	
	for(int i=2;i<=n;i++)
	{	//计算左到右单调递增序列的长度
		if(a[i-1]<a[i]) f[i]=f[i-1]+1;
	}
	for(int i=n-1;i>0;i--)
	{	//从右到左单调递增序列的长度
		if(a[i]<a[i+1]) g[i]=g[i+1]+1;
	}
	int ans=1;
	for(int i=1;i<=n;i++)
	{
		if(a[i+1]>a[i-1]||i==n) ans=max(ans,f[i-1]+g[i+1]);
		ans=max(ans,f[i]);
	}
	cout<<ans<<endl;
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lwz_159

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值