J-- DZY Loves Sequences(CF-447C

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.

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.

题意:多组数据。每组输入n,接下来输入n个数,求只能修改一个数之后的最长上升子序列的长度。

思路:先求出每个上升子序列的区间,然后枚举相邻的两个区间,情况一:如果前一个区间长度为1或后一个区间长度为1,那么这两个区间就可以合并到一起;情况二:如果前一个区间的倒数(从左向右查)第二个数与后一个区间的第一个数的差值大于1或前一个区间的最后一个数与后一个区间的第二个数的差值大于1,那么这两个区间就可以合并到一起;情况三:如果不符合以上情况,那么这两个区间就不能合并,则前一个区间只能与后一个区间的第一个数合并或前一个区间的最后一个数与后一个区间合并。

Sample Input

Input
6
7 2 3 1 5 6
Output
5
Hint

You can choose subsegment a2, a3, a4, a5, a6 and change its 3rd element (that is a4) to 4.

<span style="font-size:18px;">#include <cstdio>
#include <cmath>
#include <algorithm>
#define MAX 120000
using namespace std;
struct node
{
    int l,r,len;     //l表示该区间的左端点下标,r表该区间的右端点下标,len表示该区间的长度
} st[MAX];
int a[MAX];
int main()
{
    int n,i,top;
    while(~scanf("%d",&n))
    {
        for(i=1; i<=n; i++)
            scanf("%d",&a[i]);
        top=0;
        st[top].l=1;
        for(i=2; i<=n; i++)
        {
            if(a[i]<=a[i-1])
            {
                st[top].r=i-1;
                st[top].len=st[top].r-st[top].l+1;
                top++;
                st[top].l=i;
            }
        }
        st[top].r=i-1;
        st[top].len=st[top].r-st[top].l+1;
//        for(i=0; i<=top; i++)
//            printf("l = %d r = %d len = %d\n",st[i].l,st[i].r,st[i].len);
        int ans=st[0].len,x;
        for(int i=0; i<top; i++)
        {
            if(st[i].len==1||st[i+1].len==1||((a[st[i+1].l]-a[st[i].r-1])>=2)||((a[st[i+1].l+1]-a[st[i].r])>=2))  //前两种情况
            {
                ans=max(ans,st[i].len+st[i+1].len);
            }
            else               //第三种情况
            {
                x=max(st[i].len,st[i+1].len);
                ans=max(ans,x+1);
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}<strong>
</strong></span>



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值