zoukankan      html  css  js  c++  java
  • CodeForces 446A DZY Loves Sequences(dp)

    题意:给定一个序列a,求最长的连续子序列b的长度,在至多修改b内一个数字(可修改为任何数字)的条件下,使得b严格递增。

    分析:

    1、因为至多修改一个数字,假设修改a[i],

    2、若能使a[i] < a[i + 1] 且 a[i] > a[i - 1],则修改a[i]能得到的最长连续子序列长度为l[i - 1] + r[i + 1] + 1。

    3、若不满足条件2,则修改a[i]能得到的最长连续子序列长度应取l[i - 1] + 1(即从a[i-1]能向左延伸的最大长度加上a[i]形成的序列)和r[i + 1] + 1的最大值。

    4、枚举a[i]取最大值即可。

    #pragma comment(linker, "/STACK:102400000, 102400000")
    #include<cstdio>
    #include<cstring>
    #include<cstdlib>
    #include<cctype>
    #include<cmath>
    #include<iostream>
    #include<sstream>
    #include<iterator>
    #include<algorithm>
    #include<string>
    #include<vector>
    #include<set>
    #include<map>
    #include<stack>
    #include<deque>
    #include<queue>
    #include<list>
    #define Min(a, b) ((a < b) ? a : b)
    #define Max(a, b) ((a < b) ? b : a)
    const double eps = 1e-8;
    inline int dcmp(double a, double b){
        if(fabs(a - b) < eps) return 0;
        return a > b ? 1 : -1;
    }
    typedef long long LL;
    typedef unsigned long long ULL;
    const int INT_INF = 0x3f3f3f3f;
    const int INT_M_INF = 0x7f7f7f7f;
    const LL LL_INF = 0x3f3f3f3f3f3f3f3f;
    const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f;
    const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};
    const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};
    const int MOD = 1e9 + 7;
    const double pi = acos(-1.0);
    const int MAXN = 1e5 + 10;
    const int MAXT = 10000 + 10;
    using namespace std;
    int a[MAXN];
    int l[MAXN];//从a[i]向左延伸的长度
    int r[MAXN];//从a[i]向右延伸的长度
    int main(){
        int n;
        scanf("%d", &n);
        l[0] = 1;
        for(int i = 0; i < n; ++i){
            scanf("%d", &a[i]);
            if(i){
                if(a[i] > a[i - 1]){
                    l[i] = l[i - 1] + 1;
                }
                else{
                    l[i] = 1;
                }
            }
        }
        if(n == 1){
            printf("1\n");
            return 0;
        }
        r[n - 1] = 1;
        for(int i = n - 2; i >= 0; --i){
            if(a[i] < a[i + 1]){
                r[i] = r[i + 1] + 1;
            }
            else{
                r[i] = 1;
            }
        }
        int ans = Max(r[1], l[n - 2]) + 1;//修改a[0]或a[n-1]能得到的最长连续序列长度的最大值
        for(int i = 1; i < n - 1; ++i){
            if(a[i + 1] - a[i - 1] > 1){
                ans = Max(ans, l[i - 1] + r[i + 1] + 1);
            }
            else{
                ans = Max(ans, l[i - 1] + 1);
                ans = Max(ans, r[i + 1] + 1);
            }
        }
        printf("%d\n", ans);
        return 0;
    }
    

      

  • 相关阅读:
    线性代数学习笔记
    机器学习基石笔记
    how to design Programs 学习笔记
    programming-languages学习笔记--第2部分
    P6859 蝴蝶与花 思维 + 数据结构优化
    P6429 [COCI2010-2011#6] STEP 线段树维护最长01
    P1637 三元上升子序列 树状数组优化DP
    线段树模板3.0 区间乘
    CodeForces Global Round 11 B. Chess Cheater 贪心,处理技巧
    CodeForces Global Round 11 A. Avoiding Zero 构造
  • 原文地址:https://www.cnblogs.com/tyty-Somnuspoppy/p/6438043.html
Copyright © 2011-2022 走看看