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
  • 相关阅读:
    自学Linux Shell14.3-创建临时文件
    自学Linux Shell14.2-在脚本中使用其他文件描述符
    自学Linux Shell14.1-理解输入输出
    自学Linux Shell13.3-获得用户输入(read命令)
    自学Linux Shell13.2-选项处理(主要getopt、getopts命令)
    自学Linux Shell13.1-命令行参数
    自学Linux Shell12.8-循环实例
    自学Linux Shell12.7-控制循环break、continue命令
    自学Linux Shell12.6-嵌套循环for命令
    自学Linux Shell12.5-while、until命令
  • 原文地址:https://www.cnblogs.com/kongbb/p/10758535.html
Copyright © 2011-2022 走看看