zoukankan      html  css  js  c++  java
  • codeforces Round 63-div2-D.Beautiful Array(线性动归)

    D. Beautiful Array

    You are given an array aa consisting of nn integers. Beauty of array is the maximum sum of some consecutive subarray of this array (this subarray may be empty). For example, the beauty of the array [10, -5, 10, -4, 1] is 15, and the beauty of the array [-3, -5, -1] is 0.

    You may choose at most one consecutive subarray of aa and multiply all values contained in this subarray by xx. You want to maximize the beauty of array after applying at most one such operation.

    Input

    The first line contains two integers nn and xx (1n3105,100x1001≤n≤3⋅105,−100≤x≤100) — the length of array aa and the integer xx respectively.

    The second line contains nn integers a1,a2,,ana1,a2,…,an (109ai109−109≤ai≤109) — the array aa.

    Output

    Print one integer — the maximum possible beauty of array aa after multiplying all values belonging to some consecutive subarray xx.

    Examples
    input
    Copy
    5 -2
    -3 8 -2 1 -6
    
    output
    Copy
    22
    
    input
    Copy
    12 -3
    1 3 3 7 1 3 3 7 1 3 3 7
    
    output
    Copy
    42
    
    input
    Copy
    5 10
    -1 -2 -3 -4 -5
    
    output
    Copy
    0
    
    Note

    In the first test case we need to multiply the subarray [-2, 1, -6], and the array becomes [-3, 8, 4, -2, 12] with beauty 22([-3, 8, 4, -2, 12]).

    In the second test case we don't need to multiply any subarray at all.

    In the third test case no matter which subarray we multiply, the beauty of array will be equal to 0.

    对于每一个数,都有3种状态:

    0:当前这个数不*x,且前面的数都没*x

    1:当前这个数*x,

    2:当前这个数不*x,且前面的数有*x

    设dp[i][j]为前i个数在第i个数为j状态选取时能取得的最大值。

    那么不难得出,

    dp[i][0]依赖于dp[i-1][0],f[i][1]依赖于dp[i-1][0],dp[i-1][1],f[i][2]依赖于dp[i-1][1],dp[i-1][2]。

    因为可以舍去前一部分最大值为负数的数列不要,只取一段字串,所以每一个状态还可以依赖于0。

    那么动归方程为:

    dp[i][0]=max(0ll,dp[i-1][0])+a[i];
    dp[i][1]=max(0ll,max(dp[i-1][0],dp[i-1][1]))+a[i]*x;
    dp[i][2]=max(0ll,max(dp[i-1][1],dp[i-1][2]))+a[i];

    比较每个i的每一个状态,取其最大值,就时最终答案。

    #include <iostream>
    #include <stdio.h>
    using namespace std;
    int n,x;
    long long a[300010];
    long long dp[300010][5];
    
    int main()
    {
        std::ios::sync_with_stdio(false);
        cin>>n>>x;
        for(int i=1;i<=n;i++) cin>>a[i];
        long long ans=0;
        for(int i=1;i<=n;i++)
        {
            dp[i][0]=max(0ll,dp[i-1][0])+a[i];
            dp[i][1]=max(0ll,max(dp[i-1][0],dp[i-1][1]))+a[i]*x;
            dp[i][2]=max(0ll,max(dp[i-1][1],dp[i-1][2]))+a[i];
            ans=max(ans,max(dp[i][0],max(dp[i][1],dp[i][2])));
        }
        cout<<ans<<endl;
        return 0;
    }
  • 相关阅读:
    从零开始入门 K8s| 详解 Pod 及容器设计模式
    从零开始入门 K8s| 阿里技术专家详解 K8s 核心概念
    时间和空间的完美统一!阿里云时空数据库正式商业化
    SaaS加速器,到底加速了谁? 剖析阿里云的SaaS战略:企业和ISV不可错过的好文
    来杭州云栖大会,全面了解企业如何实现云上IT治理
    DataV教你如何给可视化应用一键美颜
    Serverless Kubernetes全面升级2.0架构:支持多命名空间、RBAC、CRD、PV/PVC等功能
    基于 APIGateway 打造生产级别的 Knative 服务
    P1434 滑雪
    P1613 跑路
  • 原文地址:https://www.cnblogs.com/BakaCirno/p/10758491.html
Copyright © 2011-2022 走看看