1082: 循环数组最大子段和 [DP]
时间限制: 1 Sec 内存限制: 128 MB题目描述
KACA在做了最大子段和问题之后,思考若数组可以首尾相接的话最大子段和应该是多少。
输入
有多组测试数据。
每一组的第一行是一个整数nn。
下面一行是nn个以空格分开的整数aiai。
1≤N≤10000
0≤|ai|≤106
输出
对于每一组数据,输出当数组可以首位相接时的最大子段和,占一行。
样例输入
6
-1 4 -1 -5 5 1
样例输出
9
思路:
本题与普通的最大子段和问题不同的是,最大子段和可以是首尾相接的情况,即可以循环。那么这个题目的最
大子段和有两种情况
(1)正常数组中间的某一段和最大。这个可以通过普通的最大子段和问题求出。
(2)此数组首尾相接的某一段和最大。这种情况是由于数组中间某段和为负值,且绝对值很大导致的,那么 我们只需要把中间的和为负值且绝对值最大的这一段序列求出,用总的和减去它就行了。
即,先对原数组求最大子段和,得到p1,然后把数组中所有元素符号取反,再求最大子段和,得到p2,
原数组的所有元素和为sum,就是求两次普通最大字段和,那么最终答案就是max(p1, sum-(-p2)。
#include<cstdio>
#include<algorithm>
#include<cmath>
#define max_n 100000
using namespace std;
typedef long long LL;
LL a[max_n],b[max_n];
int main() {
LL n;
while(scanf("%lld", &n) != EOF) {
LL p1 = 0, p2 = 0, maxn1 = 0, maxn2 = 0, sum = 0, maxn = 0;
for(int i = 0; i < n; i++) {
scanf("%lld", &a[i]);
sum += a[i];
b[i] = -a[i];
}
for(int i = 0; i < n; i++) {
p1 += a[i];
p2 += b[i];
if(p1 < 0) p1 = 0;
if(p2 < 0) p2 = 0;
maxn1 = max(p1, maxn1);
maxn2 = max(p2, maxn2);
}
maxn = max(maxn1, sum + maxn2);
printf("%lld\n", maxn);
}
return 0;
}