Maxmum subsequence sum problem
We have a lot of ways to solve the maximum subsequence sum problem, but different ways take different time.
1、Brute-force algorithm
int maxSubSum1(const vector<int> &a)
{
int maxSum=0; for(int i=0;i<a.size();i++)
for(int j=i;j<a.size();j++)
{
int sum=0;
for(int k=i;k<=j;k++)
sum+=a[k]; if(sum>maxSum)
maxSum=sum;
} return maxSum;
}
/*The running time is O(n^3)
It takes too much time.
*/
2、a little imporvement
int maxSubSum2(const vector<int>& a )
{
int maxSum=0; for(int i=0;i<a.size();i++)
{
int sum=0; for(int j=i;j<a.size();j++)
{
sum+=a[j];
if(maxSum<sum)
{
maxSum=sum;
}
}
} return maxSum;
}
3. Divide-conquer algorithm
We can divide this problem into three parts:
(1) First half;
(2) cross the middle parts;
(3) second part;
What we need to do is to find the max sum of the three part.
int max3(int a, int b, int c)
{
if(a>b)
{
if(a>c)return a;
else return c;
}
else
{
if(c>b)return c;
else return b;
}
} int maxSubSum3(cosnt vector<int >& a, int left, int right)
{
if(left==right)
if(a[left]>0) return a[left];
else return 0; int center= (left+right)/2;
int maxLeftSum=maxSumRec(a, left, center);
int maxRightSum=maxSumRec(a, center+1, right); int maxLeftBoderSum=0, leftBoderSum=0;
for(int i=center;i>=left;i--)
{
leftBoderSum+=a[i];
if(leftBoderSum>maxLeftBoderSum)
maxLeftBoderSum=leftBoderSum;
} int maxRightBoderSum=0, leftBoderSum=0;
for(int i=center+1;i<=right;i++)
{
rightBoderSum+=a[i];
if(rightBoderSum>maxRightBoderSum)
maxRightBoderSum=rightBoderSum;
} return max3(maxLeftSum, maxLeftBoderSum+maxRightBoderSum,maxRightSum);
}
4. The best algorithm
If the start is negative, the sum of the subsequence can not be the max. Hence, any negative subsequence cannot possibly be a prefix of the optimal subsequence.
int maxSubSum4(const vector<int> & a)
{
int maxSum=0, sum=0; for(int i=0;i<a.size();i++)
{
sum+=a[i]; if(sum>maxSum)
maxSum=sum;
else if(sum<0)
sum=0;
} return maxSum;
}
Maxmum subsequence sum problem的更多相关文章
- Solutions for the Maximum Subsequence Sum Problem
The maximum subarray problem is the task of finding the contiguous subarray within a one-dimensional ...
- MAXIMUM SUBSEQUENCE SUM PROBLEM
排除不合理的项(负值), 设定一个标杆sum, 往后扫描看是否有比sum好的情况. We should ensure the following conditions: 1. The result m ...
- HD2058The sum problem
The sum problem Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tot ...
- HDU 2058 The sum problem(枚举)
The sum problem Problem Description Given a sequence 1,2,3,......N, your job is to calculate all the ...
- HDU 2058:The sum problem(数学)
The sum problem Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- 【BZOJ-3638&3272&3267&3502】k-Maximum Subsequence Sum 费用流构图 + 线段树手动增广
3638: Cf172 k-Maximum Subsequence Sum Time Limit: 50 Sec Memory Limit: 256 MBSubmit: 174 Solved: 9 ...
- summary of k Sum problem and solutions in leetcode
I found summary of k Sum problem and solutions in leetcode on the Internet. http://www.sigmainfy.com ...
- Subset sum problem
https://en.wikipedia.org/wiki/Subset_sum_problem In computer science, the subset sum problem is an i ...
- HDu 1001 Sum Problem 分类: ACM 2015-06-19 23:38 12人阅读 评论(0) 收藏
Sum Problem Time Limit: 1000/500 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total ...
随机推荐
- 本地win7 把数组写入 txt 文本日志 json_encode转换中文,需要加上JSON_UNESCAPED_UNICODE 不适用unicode --仅仅支持php5.4以后
json_encode 改进 为 json_encode_ex function json_encode_ex($value){ if (version_compare(PHP_VERSION, '5 ...
- Beego学习笔记——开始
beego简介 beego是一个快速开发Go应用的http框架,他可以用来快速开发API.Web.后端服务等各种应用,是一个RESTFul的框架,主要设计灵感来源于tornado.sinatra.fl ...
- asp.net 如何引用dll
这要看你引用dll文件是什么文件,是单纯的类库还是外部或自定义控件dll文件. 如果是类库dll文件,引用的步骤是这样的:在解决方案管理器中,选中要添加引用的项目或网站-右击-添加引用-选择要添加的d ...
- C# 语言规范_版本5.0 (第6章 转换)
1. 转换 转换(conversion) 使表达式可以被视为一种特定类型.转换可导致将给定类型的表达式视为具有不同的类型,或其可导致没有类型的表达式获得一种类型.转换可以是隐式的 (implicit) ...
- Tyvj-超级书架
描述 Farmer John最近为奶牛们的图书馆添置了一个巨大的书架,尽管它是如此的大,但它还是几乎瞬间就被各种各样的书塞满了.现在,只有书架的顶上还留有一点空间. 所有N(1 <= N < ...
- ios开发的系统兼容性问题解决
对于系统中过时的方法或者是为了向下兼容兼容不同的版本使用最新的方法都要判断当前的系统版本号,在进行方法的调用 1.系统方法过时的注释 ````objc - (void)imagePickerContr ...
- Git 的版本库创建和修改
什么是版本库呢?版本库又名仓库,英文名repository,你可以简单理解成一个目录,这个目录里面的所有文件都可以被Git管理起来,每个文件的修改.删除,Git都能跟踪,以便任何时刻都可以追踪历史,或 ...
- struts2查询的数据的存放
当我们查询数据的时候,把它存放到一个位置.以供页面显示. 1:使用***Map取代内置对象存放 public String query(){ ActionContext.getContext().pu ...
- sap 设备cnsapwin不支持页格式*****
SAP SMARTFORMS 打印 CNSAPWIN 不支持页格式 解决办法: 在smartforms里的表格属性虽然定义了要打印的页格式 ZUNIA5 ,但是打印时会提示错误:" CNSA ...
- 关于C语言知识调查
因为上一篇随笔对这一部分写得不够清楚,因此在这篇做一些补充. 你是怎么学习C语言的? 起初,对于C语言的学习主要是通过老师课堂的教学,完成相关的课后作业.与我的技能相比的话,他们都有一个共同点需要去实 ...