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
  • 相关阅读:
    浮点数转换成二进制的方法
    复杂的函数声明
    任务栏上最小化程序后,应用程序仍然处于激活状态
    处理多屏
    托盘在XP下不能显示tooltip,在Vista和Windows7下正常
    第一次WM_PAINT事件执行前显示白色框 的解决办法
    IPv6地址在URL上的格式
    19 层模型中的固定定位
    18css中的相对定位
    17css中的浮动模型
  • 原文地址:https://www.cnblogs.com/kongbb/p/10758535.html
Copyright © 2011-2022 走看看