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;

}


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