描述 Description
DZY has a sequence a, consisting of n integers.
We’ll call a sequence ai, ai + 1, …, aj (1 ≤ i ≤ j ≤ n) a subsegment of the sequence a. The value (j - i + 1) denotes the length of the subsegment.
Your task is to find the longest subsegment of a, such that it is possible to change at most one number (change one number to any integer you want) from the subsegment to make the subsegment strictly increasing.
You only need to output the length of the subsegment you find.
输入格式 InputFormat
The first line contains integer n (1 ≤ n ≤ 105). The next line contains n integers a1, a2, …, an (1 ≤ ai ≤ 109).
输出格式 OutputFormat
In a single line print the answer to the problem — the maximum length of the required subsegment.
样例输入 SampleInput
5
1 1 2 3 4
## 样例输出 SampleOutput >5
代码 Code
O(n) 跑一遍就好了啊,为什么我打 cf 中国场总是很逗。。。
#include <stdio.h>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
const int inf=0x7fffffff;
int a[100005];
int f[100005][2];
int i,j,t,n,m,l,r,k,z,y,x,ans;
int main()
{
scanf("%d",&n);
for (i=1;i<=n;i++) scanf("%d",&a[i]);
a[0]=-inf;
ans=0;
for (i=1;i<=n;i++)
{
if (a[i]>a[i-1])
{
f[i][1]=f[i-1][1]+1;
f[i][0]=f[i-1][0]+1;
}
else
{
f[i][0]=1;
f[i][1]=(i>1)?2:1;
}
if (i>1 && a[i]>a[i-2]+1) f[i][1]=max(f[i][1],f[i-2][0]+2);
ans=max(ans,f[i][0]);
ans=max(ans,f[i][1]);
ans=max(ans,f[i-1][0]+1);
}
printf("%d\n",ans);
return 0;
}