#include <string.h>
#include <stdio.h>
/**
* 计算 a中从 left 到 right的最大子序列
* l r为计算得到的子序列位置
*/
int maxSub(int *a, int left, int right, int *l, int *r) {
if (left == right) {
*l = left;
*r = left;
return a[left];
}
if (left + 1 == right) {
if (a[left] > 0 && a[right] > 0) {
*l = left;
*r = right;
return a[left] + a[right];
} else if (a[left] > a[right]) {
*l = left;
*r = left;
return a[left];
} else {
*l = right;
*r = right;
return a[right];
}
}
int center = (left + right) / 2;
int ll = 0, lr = 0;
int sum1 = maxSub(a, left, center, &ll, &lr);
int rl = 0, rr = 0;
int i;
int sum2 = maxSub(a, center + 1, right, &rl, &rr);
if (sum1 > 0 && sum2 > 0) {
//计算中间数据
int tempsum = 0;
int max = rl - 1;
i = lr + 1;
for (; i <= max; i&
最大子段分治法解法
最新推荐文章于 2023-11-20 18:34:36 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)