A problem similar to the strict increment of sub-sequences. This problem can change a number into any number, so that the strict increment of the subsequence increases.
* Idea: Two mark array from[i],to[i];from[i] meaning is the longest strict increment sequence length starting from I, To[i] is the strict increment subsequence length to the end of I.
If a[i]>=a[i+1] or a[i]<=a[i-1], the length is to[i-1]+from[i+1]+1;
Dzy Loves Sequences
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's possible to change at most one number to any integer want) from the subsegment to make the subsegment strictly increasing.
You have need to output the length of the subsegment you find. Input
The first line contains integer n (1≤n≤105). The next line contains n integers a1, a2, ..., an (1≤ai≤109). Output
In a single line print the answer to the problem-the maximum length of the required subsegment. Sample Test (s) input
6
7 2 3 1 5 6
Output
5
Note
You can choose Subsegment A2, A3, A4, A5, A6 and the change it 3rd element (that's A4) to 4.
#include <stdio.h> #include <string.h> #include <algorithm> using namespace std;
int main () {int a[100005];
int from[100005];//A strictly incrementing subsequence length int point=1 that starts from I and strictly increments the subsequence length int to[100005];//to I end;
Memset (From,0,sizeof (from));
memset (To,0,sizeof (to));
int i,n;
scanf ("%d", &n);
for (i=0;i<n;i++) scanf ("%d", &a[i]);
From[n-1]=1;
for (i=n-1;i>0;i--) {if (a[i]>a[i-1]) from[i-1]=from[i]+1;
else from[i-1]=1;
}//preprocessing, similar to DP, find a state.
To[0]=1;
for (i=0;i<n-1;i++) {if (a[i]<a[i+1]) to[i+1]=to[i]+1;
else to[i+1]=1; }//preprocessing if (n==1| | n==2) printf ("%d\n", N),//special Case Else {if (a[0]>=a[1])//Special case Point=max (point,from[1]+
1); if (A[n-1]<=a[n-2]) Point=max (point,to[n-2]+1);//Special case <span style= "White-space:pre" > </SPAN&G t;else{for (i=1;i<n-1;i++) {if (A[i]<=a[i-1]) {if (a[i+1]-a[i-1]>=2)
<span style= "White-space:pre" > </span>point=max (point,to[i-1]+from[i+1]+1);
else {Point=max (point,from[i]+1);
Point=max (point,to[i-1]+1);
}} else if (A[i]>=a[i+1]) {if (a[i+1]-a[i-1]>=2)
Point=max (point,to[i-1]+from[i+1]+1);
else Point=max (point,to[i]+1);
} else Point=max (From[i]+to[i]-1,point);
}//processing of non-special data} printf ("%d\n", point);
} return 0; }