Codeforces 446A DZY Loves Sequences【dp】

来源:互联网 发布:淘宝快递合作价格表 编辑:程序博客网 时间:2024/05/16 07:00

A. DZY Loves Sequences
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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 sequencea. 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 containsn 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.

Examples
Input
67 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.


题目大意:

给你一个长度为N的序列,现在允许你改变其中任意一个元素的值(改变成的值当然也是任意的),让你从中找到一个连续递增子序列,求其最长的长度。


思路:


1、考虑dp,设定dp【i】【2】:

其dp【i】【0】表示以a【i】结尾,并且还没有改变元素值时的连续递增子序列的最大长度。

其dp【i】【1】表示以a【i】结尾,并且已经过改变元素值时的连续递增子序列的最大长度。


2、那么接下来考虑解决问题思路:
①首先我们将这个问题分成两种情况,第一种是将其中任意一个元素的值变大,第二种是将其中任意一个元素值变小。

②那么我们进行两次dp,第一次解决将元素值变大的情况,第二次解决将元素值变小的情况。

③那么最终答案就是两次dp之后维护最大值即可。


Ac代码:


#include<stdio.h>#include<string.h>#include<iostream>using namespace std;int dp[150000][2];int pos[150000];int a[150000];int main(){    int n;    while(~scanf("%d",&n))    {        memset(a,0,sizeof(a));        memset(pos,-1,sizeof(pos));        for(int i=1;i<=n;i++)        {            scanf("%d",&a[i]);        }        //先考虑让这个数变大。        memset(dp,0,sizeof(dp));        for(int i=1;i<=n;i++)        {            if(i==0)            {                dp[i][0]=1;            }            else            {                if(a[i]>a[i-1])                {                    dp[i][0]=max(dp[i][0],dp[i-1][0]+1);                    if(pos[i-1]==-1)                    {                        dp[i][1]=max(dp[i][1],dp[i-1][1]+1);                    }                    else                    {                        if(a[i]>pos[i-1])                        {                            dp[i][1]=max(dp[i][1],dp[i-1][1]+1);                        }                    }                }                else                {                    dp[i][0]=1;                    dp[i][1]=max(dp[i-1][0]+1,dp[i][1]);                    pos[i]=a[i-1]+1;                }            }        }        int output=0;        for(int i=1;i<=n;i++)        {            output=max(output,max(dp[i][0],dp[i][1]));        }        memset(dp,0,sizeof(dp));        memset(pos,-1,sizeof(pos));        for(int i=1;i<=n;i++)        {            if(i==1)            {                dp[i][0]=1;                dp[i][1]=1;                pos[i]=a[i+1]-1;            }            else            {                if(a[i]>a[i-1])                {                    dp[i][0]=max(dp[i][0],dp[i-1][0]+1);                    dp[i][1]=max(dp[i][1],dp[i-1][1]+1);                }                else                {                    dp[i][0]=1;                    if(pos[i-1]==-1)                    {                        if(i-2>=0&&a[i]-a[i-2]>=2)dp[i][1]=dp[i-2][0]+2;                        else dp[i][1]=2;                    }                    else dp[i][1]=max(dp[i][1],dp[i-1][1]+1);                }            }        }        for(int i=1;i<=n;i++)        {            output=max(output,max(dp[i][0],dp[i][1]));        }        printf("%d\n",output);    }}



0 0