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;
}