Codeforces Round #FF (Div. 2)C.DYZ Loves Sequence

一道類似求嚴格遞增子序列的題目。這道題可以改變一個數成任意數,使得嚴格遞增子序列增加。

*思路:兩個標記數組from[i],to[i];from[i]的含義是從i出發的最長的嚴格遞增序列長度,to[i]是到i結束的嚴格遞增子序列長度。

     如果a[i]>=a[i+1]或者a[i]<=a[i-1],長度爲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 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.

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 change its 3rd element (that is a4) to 4.

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int main()
{

    int a[100005];
    int from[100005];//從i出發的嚴格遞增子序列長度
    int to[100005];//到i結束的嚴格遞增的子序列長度
    int point=1;
    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;
    }//預處理,類似dp,找個狀態。
    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;
    }//預處理
    if(n==1||n==2)
        printf("%d\n",n);//特殊情況

    else
    {
        if(a[0]>=a[1])//特殊情況
            point=max(point,from[1]+1);
        if(a[n-1]<=a[n-2])
            point=max(point,to[n-2]+1);//特殊情況

     <span style="white-space:pre">	</span>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);

            }//對非特殊數據進行處理
        }
   printf("%d\n",point);
    }
    return 0;

}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章