转载:最大子段和问题(Maximum Interval Sum)
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).
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 ;
}
转载:最大子段和问题(Maximum Interval Sum)的更多相关文章
- PAT 1007 Maximum Subsequence Sum(最长子段和)
1007. Maximum Subsequence Sum (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Y ...
- 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 ...
- 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 ...
- Light OJ 1272 Maximum Subset Sum 高斯消元 最大XOR值
版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/u011686226/article/details/32337735 题目来源:problem=12 ...
- [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. ...
- [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 ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- Tautology 分类: POJ 2015-06-28 18:40 10人阅读 评论(0) 收藏
Tautology Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10428 Accepted: 3959 Descri ...
- Java fundamentals of basic IO
IO is a problem difficult to handle in various of systems because it always becomes a bottleneck in ...
- Intellij IDEA svn的使用记录
这里的忽略一直灰色的,可以进入 这里的版本控制里进行忽略选择 或者 这里进行添加 这里有三个选择 按照顺序 1.忽略指定的文件 2.忽略文件夹下所有文件 3.忽略符合匹配规则的文件 到Commit C ...
- HDU 5615 Jam's math problem
Jam's math problem Problem Description Jam has a math problem. He just learned factorization.He is t ...
- Outlook 无法更新全球通讯簿,错误 0×80190194
当 Outlook 客户端尝试更新全球通讯簿,实际上是下载脱机通讯簿(Officeline Address Book,简称 OAB)时,可能会收到 0×80190194 的错误.错误代码 0×8019 ...
- .Net面試題
初级.NET开发人员 - 任何使用.NET的人都应知道的 1. 描述线程与进程的区别? 进程是系统所有资源分配时候的一个基本单位,拥有一个完整的虚拟空间地址,并不依赖线程而独立存在.进程可以定义程序的 ...
- 【转载】.NET面试题系列[0] - 写在前面
原文:.NET面试题系列[0] - 写在前面 索引: .NET框架基础知识[1] - .NET框架基础知识(1) http://www.cnblogs.com/haoyifei/p/5643689.h ...
- 先来个xmpp学习连接
http://my.oschina.net/SoulJa/blog?catalog=3340253&temp=1468228088114 http://my.oschina.net/iOSli ...
- win8下安装matlab7.0
在win8下安装matlab7.0会出现一些兼容性的问题,需要设置系统环境变量,修改方式如下. 1.设置环境变量,方法:在你的安装目录的\MATLAB7\bin\win32有一个叫做atlas_Ath ...
- 在ubuntu上配置apue的运行环境
http://www.apuebook.com/code3e.html 在上面的网站下载代码包,解压得到源码 sudo apt-get install libbsd-dev 安装这个支持,在解压包的m ...