zoukankan      html  css  js  c++  java
  • Educational Codeforces Round 63 D. Beautiful Array

    D. Beautiful Array
    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    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,100x100) — the length of array aa and the integer xx respectively.

    The second line contains nn integers a1,a2,,an (109ai109) — the array aa.

    Output

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

    Examples
    input
    5 -2
    -3 8 -2 1 -6
    
    output
    22
    
    input
    12 -3
    1 3 3 7 1 3 3 7 1 3 3 7
    
    output
    42
    
    input
    5 10
    -1 -2 -3 -4 -5
    
    output
    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.

     果然还是太菜了,简单dp都想不出来。

    dp[0] 记录不使用x的最大值

    dp[1] 记录允许使用x的情况下的最大值

    dp[2]记录在前面的区间中已经使用了x的情况下的最大值(尽管可能使用x的区间对最大值并没有贡献)

    #include<cstdio>
    #include<algorithm>
    using namespace std;
    
    long long arr[300005];
    
    int main(){
        int n, x;
        long long dp[3] = {},ans=0;
        scanf("%d%d",&n,&x);
        for(int i=0;i<n;i++){
            scanf("%I64d",&arr[i]);
            dp[0] = max(0ll,dp[0]+arr[i]);
            dp[1] = max(dp[0],dp[1]+arr[i]*x);
            dp[2] = max(dp[1],dp[2]+arr[i]);
            ans = max(ans,dp[2]);
    
        }
        printf("%I64d
    ",ans);
        return 0;
    
    }
    View Code
  • 相关阅读:
    相似矩阵
    特征值和特征向量
    非齐次方程组的通解
    线性方程组解的性质和结构、基础解系
    高斯消元法求齐次线性方程的通解
    从零开始学 Web 之 HTML(三)表单
    从零开始学Web之HTML(二)标签、超链接、特殊符号、列表、音乐、滚动、head等
    从零开始学 Web 之 HTML(一)认识前端
    JavaScript基础(一)概述
    如何恢复windows的exe文件的默认打开方式
  • 原文地址:https://www.cnblogs.com/kongbb/p/10758535.html
Copyright © 2011-2022 走看看