zoukankan      html  css  js  c++  java
  • cf446A DZY Loves Sequences

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

    题意是有一串序列,可以把其中的一个数修改成任意值,求这样改完以后最长上升子序列长度

    三维dp:前i个、是否用掉第i个、是否和以前的相连的最长长度

    #include<iostream>
    #include<cstdio>
    #include<cstdlib>
    #include<algorithm>
    #include<cmath>
    #define inf 0x7fffffff
    #define ll long long
    using namespace std;
    inline ll read()
    {
        ll x=0,f=1;char ch=getchar();
        while(ch>'9'||ch<'0'){if(ch=='-')f=-1;ch=getchar();}
        while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
        return x*f;
    }
    int n,ans;
    int a[100005],f[100005][2][2];//used? last?
    int main()
    {
        n=read();
        for(int i=1;i<=n;i++)
            a[i]=read();
        for(int i=1;i<=n;i++)
        {
        	f[i][0][0]=f[i][1][1]=1;
    		if(i!=1)f[i][1][0]=2;
            if(a[i]>a[i-1])
    		{
    		    f[i][0][0]=max(f[i][0][0],f[i-1][0][0]+1);
    		    f[i][1][0]=max(f[i][1][0],f[i-1][1][0]+1);
    		    f[i][1][1]=max(f[i][1][1],f[i-1][0][0]+1);
    		}
    		else 
    		{
    			f[i][1][1]=max(f[i][1][1],f[i-1][0][0]+1);
    		}
    		if(i!=1&&a[i]>a[i-2]+1)f[i][1][0]=max(f[i][1][0],f[i-1][1][1]+1);
    		ans=max(ans,f[i][0][0]);
    		ans=max(ans,f[i][1][0]);
    		ans=max(ans,f[i][1][1]);
    	}
    	printf("%d",ans);
    }
    
    ——by zhber,转载请注明来源
  • 相关阅读:
    NGUI的HUD Text的扩展插件学习--(HUDText)的使用
    C#设计模式:外观模式(Facade Pattern)
    NGUI的HUD Text的扩展插件学习--(UIFollowTarget)的使用
    NGUI的怎么在一个Gameobject(游戏物体)中调用另一个Gameobject(游戏物体)的脚本(C#)
    C#设计模式:组合模式(Composite Pattern)
    C#Contains方法的错误理解
    C#.NET动态页面静态化生成
    C++ primer plus读书笔记——第1章 预备知识
    如何判断一个数是2的幂
    C++将数值转换为string
  • 原文地址:https://www.cnblogs.com/zhber/p/4036052.html
Copyright © 2011-2022 走看看