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;
    }
  • 相关阅读:
    洛谷P1724 东风谷早苗
    hdu 1001 Sum Problem
    洛谷 P1006 传纸条
    codevs 6116 区间素数
    LibreOJ #101. 最大流
    洛谷 P1455 搭配购买
    LibreOJ #119. 最短路 (堆优化dijkstra)
    LibreOJ #109. 并查集
    COGS.1200 ganggang的烦恼
    uoj #15. 【NOIP2014】生活大爆炸版石头剪刀布
  • 原文地址:https://www.cnblogs.com/EchoZQN/p/10908427.html
Copyright © 2011-2022 走看看