一.问题描述
 
      给定长度为n的整数序列,a[1...n], 求[1,n]某个子区间[i , j]使得a[i]+…+a[j]和最大.或者求出最大的这个和.
      例如(-2,11,-4,13,-5,2)的最大子段和为20,所求子区间为[2,4].
      如果该序列的所有元素都是负整数时定义其最大子段和为0。
 
 二. 问题分析
      1、最大子段和问题的简单算法:
 
 
 
 
      2、最大子段和问题的分治法:
 
      求子区间及最大和,从结构上是非常适合分治法的,因为所有子区间[start, end]只可能有以下三种可能性:
      在[1, n/2]这个区域内
      在[n/2+1, n]这个区域内
      起点位于[1,n/2],终点位于[n/2+1,n]内
 int DAC(int * array, int left, int right)
{
if (left == right)
return array[left]> ? array[left] : ; int center = ( left + right ) / ;
int leftSum = DAC(array, left, center);
int rightSum = DAC(array, center+, right); int temp = ;
int leftHalfMaxSum = ;
for (int i=center;i>=left;--i)
{
temp += array[i];
if (leftHalfMaxSum < temp)
leftHalfMaxSum = temp;
}
temp = ;
int rightHalfMaxSum = ;
for (int i=center+;i<=right;++i)
{
temp += array[i];
if (rightHalfMaxSum < temp)
rightHalfMaxSum = temp;
} int max = leftSum > rightSum ? leftSum : rightSum;
return max > leftHalfMaxSum + rightHalfMaxSum ? max : leftHalfMaxSum + rightHalfMaxSum;
}

分治法的难点在于第三种情形的理解,这里应该抓住第三种情形的特点,也就是中间有两个定点,然后分别往两个方向扩张,以遍历所有属于第三种情形的子区间,求的最大的      一个,如果要求得具体的区间,稍微对上述代码做点修改即可. 分治法的计算时间复杂度为O(nlogn).

    3、最大子段和问题的动态规划算法:
      令b[j]表示以位置 j 为终点的所有子区间中和最大的一个
      子问题:如j为终点的最大子区间包含了位置j-1,则以j-1为终点的最大子区间必然包括在其中
      如果b[j-1] >0, 那么显然b[j] = b[j-1] + a[j],用之前最大的一个加上a[j]即可,因为a[j]必须包含
      如果b[j-1]<=0,那么b[j] = a[j]。
 
      对于这种子问题结构和最优化问题的证明,可以参考算法导论上的“剪切法”,即如果不包括子问题的最优解,把你假设的解粘帖上去,会得出子问题的最优化矛盾.证明如下:
      令a[x,y]表示a[x]+…+a[y] , y>=x
      假设以j为终点的最大子区间 [s, j] 包含了j-1这个位置,以j-1为终点的最大子区间[ r, j-1]并不包含其中
      即假设[r,j-1]不是[s,j]的子区间
      存在s使得a[s, j-1]+a[j]为以j为终点的最大子段和,这里的 r != s 
      由于[r, j -1]是最优解, 所以a[s,j-1]<a[r, j-1],所以a[s,j-1]+a[j]<a[r, j-1]+a[j]
      与[s,j]为最优解矛盾.
 int DP(int *a, int size)
{
int *b = new int[size];
b[] = a[];
int max = b[];
for (int i=;i<size;++i)
{
if (b[i-] > )
b[i] = b[i-] + a[i];
else
b[i] = a[i]; if(b[i]>max)
max = b[i];
}
return max;
}

测试代码:

 #include "stdafx.h"
#include <stdlib.h>
#include "DivideAndConquer.h"
#include "DynamicProgramming.h" int _tmain(int argc, _TCHAR* argv[])
{
int array[] = {-, , -, , -, -};
//int result = DAC(array, 0, 5);
int result = DP(array, );
printf("%d", result);
system("pause");
return ;
}

转自:http://blog.csdn.net/jiyanfeng1/article/details/8058604

转载:最大子段和问题(Maximum Interval Sum)的更多相关文章

  1. PAT 1007 Maximum Subsequence Sum(最长子段和)

    1007. Maximum Subsequence Sum (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Y ...

  2. leetcode–Binary Tree Maximum Path Sum

    1.题目说明 Given a binary tree, find the maximum path sum.   The path may start and end at any node in t ...

  3. PAT甲 1007. Maximum Subsequence Sum (25) 2016-09-09 22:56 41人阅读 评论(0) 收藏

    1007. Maximum Subsequence Sum (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Y ...

  4. Light OJ 1272 Maximum Subset Sum 高斯消元 最大XOR值

    版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/u011686226/article/details/32337735 题目来源:problem=12 ...

  5. [LeetCode] Binary Tree Maximum Path Sum 求二叉树的最大路径和

    Given a binary tree, find the maximum path sum. The path may start and end at any node in the tree. ...

  6. [leetcode]Binary Tree Maximum Path Sum

    Binary Tree Maximum Path Sum Given a binary tree, find the maximum path sum. The path may start and ...

  7. LeetCode(124) Binary Tree Maximum Path Sum

    题目 Given a binary tree, find the maximum path sum. For this problem, a path is defined as any sequen ...

  8. LeetCode124:Binary Tree Maximum Path Sum

    题目: Given a binary tree, find the maximum path sum. The path may start and end at any node in the tr ...

  9. leetcode 124. Binary Tree Maximum Path Sum

    Given a binary tree, find the maximum path sum. For this problem, a path is defined as any sequence ...

随机推荐

  1. django 自定义标签和过滤器

    django 自定义标签和过滤器 Django支持自定义标签和过滤器.起初还不太重视它这项功能,但最近试了试自定义标签.发现django这个功能实在是太爽了. 首先在你项目的一个app中建立一个pyt ...

  2. c#记事本

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  3. LTE切换与TAU问题

    假如有两个LTE基站A B(同频组网) AB TAC不同 且添加了双向邻区关系 现终端开机重选至A然后往B方向移动 是先切换呢?还是先进性TAU更新 这个没有影响,,TAU并非需要在IDLE状态下才能 ...

  4. 制作透明色:《CSS3 RGBA》与Opacity样式用法

    前面我们一起探讨了一下CSS3 Gradient(css3 渐变),今天我们一起来探讨一下CSS3中的RGBA.RGB对于大家来说一点不陌生,他就是红色R+绿色G+蓝色B,那现在我们所说的RGBA又是 ...

  5. [Emacs] 常用快捷键-- 生存指南

    Emacs 常用快捷键--生存指南 主要用来记录自己常用到的快捷键,记住这些快捷键可以保证你在Emacs中生存. 有可能不全,但是够用了(简单写文本). 保存和退出 使用 C-x C-s 保存文件. ...

  6. Testing Round #12 A

    A. Divisibility time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...

  7. 2016年11月26日 星期六 --出埃及记 Exodus 20:17

    2016年11月26日 星期六 --出埃及记 Exodus 20:17 "You shall not covet your neighbor's house. You shall not c ...

  8. PHP 之 FastCGI 与 mod_php 详解

    背景 PHP最常用的方式是以模块的方式(mod_php)运行在Apache中,也是Apache运行PHP的默认方式:但在Nginx中,Nginx又使用的是PHP-FPM,但是PHP-FPM到底是个什么 ...

  9. linux下的./本质

    不知道从什么时候对于./的感觉就是这是一条运行命令,因为你要运行某个文件的时候就用./ 但是这个显然是错误的./表述的是当前目录 .就是表示当前目录的.至于为什么运行当前目录下的 文件需要加上./原因 ...

  10. Struts2的标签库(三)——控制标签

    Struts2的标签库(三) --控制标签 1.if/elseif/else标签 用于分支控制,取代JSP中的if语句,根据一个boolean(test属性的值)值判断是否进行下一步运算或者输出等. ...