转载:最大子段和问题(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 ...
随机推荐
- hdu1074 Doing Homework
这题比较有意思,暴力搜索必然tle,可以用状态压缩dp解决. 我们先不考虑完成所有作业的扣分,而考虑其一个子集的情况. 假设我们得到了完成某子集S对应的作业最少扣分,我们试着向该子集中增加一个元素a, ...
- CentOS 修改线程数限制等(limits.conf)
修改/etc/security/limits.conf,例如启动程序的用户为webadmin,则添加以下配置: webadmin - nofile 65536 webadmin - nproc 655 ...
- Datagrid扩展方法InitEditGrid{支持单元格编辑}
//-----------------------------------------------------------------/******************************** ...
- java.util.zip对zip文件解压
//通过构造方法,来创建一个新的ZIP输入流 ZipInputStream in = new ZipInputStream(new FileInputStream("G:/jquery.ca ...
- MUI 微信 和支付宝支付 (前台代码)
<!-- 校园公告详情界面 用于显示校园公告的详情信息 在校园公告界面点击某一条目后 进入本界面查看详情 --> <!DOCTYPE html> <html> &l ...
- zookeeper系列之七—从远程调用认识zookeeper
http://www.csdn.net/article/2014-01-02/2817944-zookeeper 在Hadoop的学习过程中,Zookeeper是让很多初学者困惑的技术,远程调用服务是 ...
- iptraf:TCP/UDP网络监控工具
原文:http://www.unixmen.com/iptraf-tcpudp-network-monitoring-utility/ 作者: Enock Seth Nyamador 译文:LCTT ...
- SD卡驱动分析(一)
Android下的SD卡驱动与标准LINUX下的SD卡驱动好像没有太大的区别,这里就以高通的ANDROID 2.3以代表,来简要分析一下LINUX下SD卡驱动的写法.由于小弟的技术有限,分析的有错的地 ...
- 【转载】如何系统地自学 Python?
原文:如何系统地自学 Python? 作者:彭猫 本文由 知乎 彭猫 授权发布,版权所有归作者,转载请联系作者! 是否非常想学好 Python,一方面被琐事纠缠,一直没能动手,另一方面,担心学习成本太 ...
- JSP学习——语法
JSP模版元素 JSP表达式 JSP脚本片断 JSP注释JSP指令JSP标签 JSP内置对象如何查找JSP页面中的错误 1:JSP模版元素 : JSP页面中的HTML内容称之为JSP模版元素. JSP ...