codeforce 33C Wonderful Randomized Sum(最大子段和,dp)

C. Wonderful Randomized Sum
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output

Learn, learn and learn again — Valera has to do this every day. He is studying at mathematical school, where math is the main discipline. The mathematics teacher loves her discipline very much and tries to cultivate this love in children. That’s why she always gives her students large and difficult homework. Despite that Valera is one of the best students, he failed to manage with the new homework. That’s why he asks for your help. He has the following task. A sequence of n numbers is given. A prefix of a sequence is the part of the sequence (possibly empty), taken from the start of the sequence. A suffix of a sequence is the part of the sequence (possibly empty), taken from the end of the sequence. It is allowed to sequentially make two operations with the sequence. The first operation is to take some prefix of the sequence and multiply all numbers in this prefix by  - 1. The second operation is to take some suffix and multiply all numbers in it by  - 1. The chosen prefix and suffix may intersect. What is the maximum total sum of the sequence that can be obtained by applying the described operations?

Input

The first line contains integer n (1 ≤ n ≤ 105) — amount of elements in the sequence. The second line contains n integers ai ( - 104 ≤ ai ≤ 104) — the sequence itself.

Output

The first and the only line of the output should contain the answer to the problem.

Examples

inputCopy

3
-1 -2 -3

outputCopy

6

inputCopy

5
-4 2 0 5 0

outputCopy

11

inputCopy

5
-1 10 -5 10 -2

outputCopy

18
题意:

给定一串数,可以通过修改前缀和后缀(乘以-1),求这段序列的最大和

思路:

非常有意思的一道题,分别设前缀,中间部分,后缀分别为 a , b , c a,b,c a,b,c,那么有 a + b + c = S a+b+c=S a+b+c=S S S S为这段序列原始的和,现在要求的是: − ( a + c ) + b -(a+c)+b (a+c)+b的最大值,因为 − ( a + c ) + b = − ( S − b ) + b = − S + 2 ∗ b -(a+c)+b=-(S-b)+b=-S+2*b (a+c)+b=(Sb)+b=S+2b,问题转化为求 b b b的最大值,而 b b b正好表示原始序列的部分和。

在求解原始序列的最大子串和时,设 d p [ i ] dp[i] dp[i]表示序列从1,…i的最大值,那么有 d p [ i ] = m a x ( d p [ i − 1 ] + a [ i ] , a [ i ] ) dp[i]=max(dp[i-1]+a[i],a[i]) dp[i]=max(dp[i1]+a[i],a[i]),由于 d p [ i − 1 ] dp[i-1] dp[i1] a [ i ] a[i] a[i]均可能为负数,所以最大子串和不能直接用 d p [ n ] dp[n] dp[n]维护(比如例子[233,233,-466,2],最大子串和并不是 d p [ 4 ] = 2 dp[4]=2 dp[4]=2,而是前面的部分和466,所以利用maxv维护一下从1,…i的dp[i]最大值。

更多请参考https://www.luogu.org/problemnew/solution/CF33C

#include<bits/stdc++.h>
using namespace std;
int a[100010];
int sum[100010];
int dp[100010];
int main(){
	int n;scanf("%d",&n);
	for(int i=1;i<=n;i++){
		scanf("%d",&a[i]);
		sum[i]=sum[i-1]+a[i];
	}
	
	dp[1]=a[1];
	int maxv=-0x3f3f3f3f;
	for(int i=1;i<=n;i++){
		dp[i]=max(dp[i-1]+a[i],a[i]);
		maxv=max(maxv,dp[i]);
	}
	
	maxv=max(maxv,0);
	printf("%d\n",-sum[n]+2*maxv);
	return 0;
}
posted @ 2019-05-14 22:13  xzhws  阅读(57)  评论(0编辑  收藏  举报