题目描述
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;
}