转载:最大子段和问题(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 ...
随机推荐
- ThinkPhp之分页
function page($count,$pagesize=3){ $Page=new \Think\Page($count,$pagesize); //每页显示记录数 $Page->setC ...
- poj2429 GCD & LCM Inverse
用miller_rabin 和 pollard_rho对大数因式分解,再用dfs寻找答案即可. http://poj.org/problem?id=2429 #include <cstdio&g ...
- The 3n + 1 problem 分类: POJ 2015-06-12 17:50 11人阅读 评论(0) 收藏
The 3n + 1 problem Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 53927 Accepted: 17 ...
- 知乎上有一个问题“在mfc框架中,有上面方法能直接将opencv2.0库中的Mat格式图片传递到Picture Control”中显示?
一直以来,我使用的方法都是shiqiyu在opencvchina上面提供的引入directshow,并且采用cvvimage和cameraDs的方法.这个方法虽然在xp/win7/win8下面都能够成 ...
- An Easy C Program Problem
找幸运数 题目描述 数字8最多的那个数为幸运数. 输入n和n个整数,找这n个数中的幸运数.在主函数中调用ndigit函数,判断某个整数x含数字8的个数.如果有多个幸运数输出第一个幸运数,如果所有的数中 ...
- Educational Codeforces Round 16 A
Description he only king stands on the standard chess board. You are given his position in format &q ...
- Java中Properties类的操作配置文件
知识学而不用,就等于没用,到真正用到的时 候还得重新再学.最近在看几款开源模拟器的源码,里面涉及到了很多关于Properties类的引用,由于Java已经好久没用了,而这些模拟器大多用 Java来写, ...
- Android编译中m、mm、mmm的区别
准备工作 在AndroidSource Code中有envsetup.sh档案,当执行过此档案后,可以大幅将build的过程简单化.自动化 此档案在src(android source code 位置 ...
- C++ 函数后加const
1.非静态成员函数后面加const(加到非成员函数或静态成员后面会产生编译错误)2.表示成员函数隐含传入的this指针为const指针,决定了在该成员函数中, 任意修改它所在的类的成员的操作都是 ...
- [转]Unity3d之MonoBehaviour的可重写函数整理
最近在学习Unity3d的知识.虽然有很多资料都有记录了,可是我为了以后自己复习的时候方便就记录下来吧!下面的这些函数在Unity3d程序开发中具有很重要的作用. Update 当MonoBehavi ...