转载:最大子段和问题(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 ...
随机推荐
- c# UDP广播
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- 谷歌Chrome浏览器如何设置网页的默认编码方法
设置->高级->自定义字体->编码->utf-8
- 探索Win32系统之窗口类(转载)
Window Classes in Win32 摘要 本文主要介绍win32系统里窗口类的运做和使用机制,探索一些细节问题,使win32窗口类的信息更加明朗化. 在本文中,"类", ...
- Solr开发文档
转载:http://www.cnblogs.com/hoojo/archive/2011/10/21/2220431.html Solr 是一种可供企业使用的.基于 Lucene 的搜索服务器,它支持 ...
- Intellij IDEA svn的使用记录
这里的忽略一直灰色的,可以进入 这里的版本控制里进行忽略选择 或者 这里进行添加 这里有三个选择 按照顺序 1.忽略指定的文件 2.忽略文件夹下所有文件 3.忽略符合匹配规则的文件 到Commit C ...
- Android属性动画完全解析(中)
转载:http://blog.csdn.net/guolin_blog/article/details/43536355 大家好,在上一篇文章当中,我们学习了Android属性动画的基本用法,当然也是 ...
- DataGridView的自定义列排序
1,将需要进行排序的列做属性的设置 this.colUserName.SortMode = DataGridViewColumnSortMode.Programmatic; 2,添加列的事件 //点击 ...
- 启动Print Spooler服务提示:"错误1068,依存服务或无法启动"
本人windows8操作系统,从网上看到在运行中输入 sc config spooler depend= rpcss 即可,试了下果然有效.具体原因待了解.
- JAVA题目
1.在项目中创建Number类,判断字符串"mingrikejijiavabu"中字符"i"出现了几次,并将结果输出. 方法一: public class Nu ...
- Windows下通过bat脚本实现自动上传文件到ftp服务器
@Echo Off Echo open ip_address [port] >ftp.up Echo [username]>>ftp.up Echo [password]>&g ...