zoukankan      html  css  js  c++  java
  • D. Beautiful Array DP

    https://codeforces.com/contest/1155/problem/D

    这个题目还是不会写,挺难的,最后还是lj大佬教我的。

    这个题目就是要分成三段来考虑,

    第一段就是不进行乘,就是直接求这一个区间的最大的一段区间的最大值。

    第二段就是后面有一部分进行乘法,前面有一部不进行乘法。这个也同样求一个区间的最大值。

    第三段就是前面有一部分和后面有一部分不进行乘法,中间有一部分进行乘法,同样求这个区间的最大值。

    #include<cstdio>
    #include<iostream>
    #include<algorithm>
    #include<cmath>
    #include<cstring>
    #include<string>
    #include<queue>
    #include<vector>
    #define inf 0x3f3f3f3f
    #define debug(x) cout<<"-----"<<" x = "<<x<<"-----"<<endl
    using namespace std;
    typedef long long ll;
    const int maxn = 3e5 + 10;
    ll dp1[maxn], dp2[maxn], dp3[maxn];
    ll a[maxn];
    
    ll max_(ll x,ll y,ll z)
    {
        ll ans = max(x, y);
        return max(ans, z);
    }
    
    int main()
    {
        int n, k;
        ll ans = 0;
        scanf("%d%d", &n, &k);
        for (int i = 1; i <= n; i++) scanf("%lld", &a[i]);
        for (int i = 1; i <= n; i++)
        {
            dp1[i] = a[i] + max(1ll * 0, dp1[i - 1]);
            ans = max(ans,dp1[i]);
        }
        for (int i = 1; i <= n; i++)
        {
            dp2[i] = a[i] * k + max_(dp1[i - 1], dp2[i - 1],0);
            ans = max(dp2[i], ans);
        }
        dp3[2] = dp2[1] + a[2];
        ans = max(ans, dp3[2]);
        for (int i = 3; i <= n; i++)
        {
            dp3[i] = a[i] + max_(dp2[i - 1], dp3[i - 1],0);
            ans = max(ans, dp3[i]);
        }
        printf("%lld
    ", ans);
        return 0;
    }
  • 相关阅读:
    AirFlow性能调优
    AirFlow通过账号密码➕角色权限控制来登陆
    通过Supervisor管理airflow的webserver进程和scheduler进程
    CSS基础一
    html学习
    Linux下MySQL远程链接配置
    搭建Firekylin博客
    Vuejs开发环境搭建及热更新
    templates页面超链接访问Controller方法
    Chrome搜索设置
  • 原文地址:https://www.cnblogs.com/EchoZQN/p/10908427.html
Copyright © 2011-2022 走看看