cf 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 a
consisting of n 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 a and multiply all values contained in this subarray by x.
You want to maximize the beauty of array after applying at most one such operation.
Input

The first line contains two integers n
and x (1≤n≤3⋅10^5,−100≤x≤100) — the length of array a and the integer x

respectively.

The second line contains n
integers a1,a2,…,an (−10^ 9≤ai≤10^9) — the array a.

Output

Print one integer — the maximum possible beauty of array a
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.

中文:
给你n个整数a[i],然后给你一个数x,你可以将其中某个连续的子段乘上x,只能乘一次,现在让你求这个序列的最大字段和。

代码:

#include<bits/stdc++.h>
    using namespace std;
    const int maxn=300005;
    typedef long long ll;
    ll v[maxn];
    ll dp[maxn][3];
    int n, x;
     
     
    int main()
    {
    	ios::sync_with_stdio(false);
    	while (cin >> n >> x)
    	{
    		memset(dp, 0, sizeof(dp));
    		for (int i = 1; i <= n; i++)
    			cin >> v[i];
    		ll ans = 0;
    		for (int i = 1; i <= n; i++)
    		{
    			dp[i][0] = max(dp[i - 1][0] + v[i], v[i]);
    			ans = max(ans, dp[i][0]);
    			dp[i][2] = max(dp[i - 1][0] + v[i] * x, max(dp[i - 1][2] + v[i] * x, v[i] * x));
    			ans = max(ans, dp[i][2]);
    			dp[i][1] = max(dp[i - 1][1] + v[i], dp[i - 1][2] + v[i]);
    			ans = max(ans, dp[i][1]);
    		}
    		cout << ans << endl;
    	}
     
    	//system("pause");
    	return 0;
    }

思路:

很不错的一道最大子段和的改进题目。

原始的最大字段和的求解策略如下

设置 dp[i]dp[i]表示计算前i个数时计算最大子段和的状态(注意最大字段和地推的过程中每次得到的dp[i]dp[i]并非为当前的最优解)
由于时最大字段和,那么当前下标的数值时必须要取的,那么状态转移的情况有两种,分别是只取当前的a[i]作为计算到dp[i]dp[i]的最大状态,以及将a[i]a[i]加到之前累加的序列上dp[i1]+a[i]dp[i-1]+a[i]

这道题目当中可以对计算过程中的任意一个序列乘以一个数xx

假设先考虑有两种情况
dp[i][2]dp[i][2]
其中dp[i][0]dp[i][0]仅记录不对任何一个子序列乘以xx时得到的最优结果,即原始的最大字段和
那么dp[i][1]dp[i][1]就需要记录序列乘以xx的结果,这样才能不漏解
状态转移方程需要考虑的情况如下

如果在dp[i][1]dp[i][1]之前就已经有xx乘以某一个序列了,那么当前的值a[i]a[i]需要考虑两种情况

一种是直接加到状态dp[i1][1]dp[i-1][1]上,即dp[i1][1]+a[i]dp[i-1][1]+a[i]
另一种是需要将a[i]a[i]也乘上一个xx,但是这种情况有个问题,因为xx只能被乘上一次;

如果a[i1]a[i-1]这个数已经被乘过xx了,那么a[i]a[i]可以直接乘以xx,即与之前的序列连在一起乘上xx,表示为dp[i][1]=dp[i1][1]+a[i]xdp[i][1]=dp[i-1][1]+a[i]*x;(1)

如果xx是在a[i1]a[i-1]之前乘过的,需要把上次乘过的xx取消掉,可以表示为dp[i][1]=dp[i1][0]+a[i]xdp[i][1]=dp[i-1][0]+a[i]*x。(2)

上面的递推关系有个问题,你怎么知道(1)上一步的状态是不是乘过xx

这里解决办法为添加一个状态,将问题分解。
重新定义状态dp[i][1]dp[i][1]dp[i][2]dp[i][2]
其中dp[i][1]dp[i][1]表示在ii之前乘过xx,但是在第ii个状态没有乘过
dp[i][2]dp[i][2]表示第i个状态正在乘以xx,后面可以接上a[i]xa[i]*x

那么状态转移方程就可以写成如下的三个形式,不漏掉任何一个解

dp[i][0]=max(dp[i1][0]+a[i],a[i]); dp[i][0] = max(dp[i - 1][0] + a[i], a[i]);

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

dp[i][1]=max(dp[i1][1]+a[i],dp[i1][2]+a[i]); dp[i][1] = max(dp[i - 1][1] + a[i], dp[i - 1][2] + a[i]);

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章