• A. DZY Loves Sequences


    题目链接:http://codeforces.com/problemset/problem/446/A

    题目大意:让你找到一个区间,你可以改变这个区间的一个数,然后使得这个区间是严格上升的

    且这个区间一定是最长的,输出区间长度

    思路:

    用dp1[i] 记录 i 之前(包括 i 自己) 连续序列长度( 不一定是最长的)

    用dp2[i] 记录 i 之后(包括 i 自己)连续序列长度 (不一定是最长的)

    然后再找到数组的一个位置 a[i+1] - i > a[i-1]  (说明a[i] 可以改变) 那么其连续子序列长度 dp1[i-1] + dp2[i+1] + 1

    AC代码:

     1 #include <cstdio>
     2 #include <string>
     3 #include <iostream>
     4 #include <algorithm>
     5 #include <cstdbool>
     6 #include <string.h>
     7 #include <math.h>
     8 
     9 
    10 using namespace std;
    11 
    12 const int maxn = 1e5+5;
    13 int a[maxn];
    14 int dp1[maxn],dp2[maxn];
    15 
    16 int main()
    17 {
    18     int n;
    19     int ans = 0;
    20     cin >> n;
    21     for (int i=1;i<=n;i++)
    22     {
    23         cin >> a[i];
    24     }
    25     if (n == 1)
    26     {
    27         printf("1
    ");
    28         return 0;
    29     }
    30     if (n == 2)
    31     {
    32         printf("2
    ");
    33         return 0;
    34     }
    35     for (int i=1;i<=n;i++)
    36     {
    37         dp1[i] = 1;
    38         if (a[i] > a[i-1])
    39             dp1[i] = dp1[i-1] + 1;
    40         ans = max(ans,dp1[i]);
    41     }
    42     for (int i=n;i>=1;i--)
    43     {
    44         dp2[i] = 1;
    45         if (a[i] < a[i+1])
    46             dp2[i] = dp2[i+1] + 1;
    47         ans = max(ans,dp2[i]);
    48     }
    49     for (int i=1;i<=n;i++)
    50     {
    51         if (a[i-1] < a[i+1] - 1)
    52             ans = max(ans,dp1[i-1]+dp2[i+1]+1);
    53     }
    54     for (int i=2;i<=n;i++)
    55         ans = max(ans,dp2[i]+1);
    56     for (int i=1;i<n;i++)
    57         ans = max(ans,dp1[i]+1);
    58     printf("%d
    ",ans);
    59     return 0;
    60 }
  • 相关阅读:
    说说 Java 线程间通信
    Java 内存模型与内存结构
    Spring Boot 整合 Shiro
    HashMap 实现原理
    Spring Boot 自动配置原理
    Spring Cloud 全链路追踪实现
    JVM 类加载机制
    volatile 关键字的作用
    Spring Boot 整合 Redis
    Docker命令
  • 原文地址:https://www.cnblogs.com/-Ackerman/p/11170192.html
Copyright © 2020-2023  润新知