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的更多相关文章

  1. Solutions for the Maximum Subsequence Sum Problem

    The maximum subarray problem is the task of finding the contiguous subarray within a one-dimensional ...

  2. MAXIMUM SUBSEQUENCE SUM PROBLEM

    排除不合理的项(负值), 设定一个标杆sum, 往后扫描看是否有比sum好的情况. We should ensure the following conditions: 1. The result m ...

  3. HD2058The sum problem

    The sum problem Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tot ...

  4. HDU 2058 The sum problem(枚举)

    The sum problem Problem Description Given a sequence 1,2,3,......N, your job is to calculate all the ...

  5. HDU 2058:The sum problem(数学)

    The sum problem Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  6. 【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 ...

  7. 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 ...

  8. Subset sum problem

    https://en.wikipedia.org/wiki/Subset_sum_problem In computer science, the subset sum problem is an i ...

  9. 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 ...

随机推荐

  1. scip习题(1) scheme和c实现的对比

    习题1.3 定义一个过程,它以三个数为参数,返回其中较大的两个数的平方和. (Define a procedure thats three numbers as argument and return ...

  2. HTML form的一些属性(第一版)

    p,li { white-space: pre-wrap } HTML表单属性总结(第一版) 基本格式为:<input type="类型" name="名字[唯一, ...

  3. Exchange 2010 实用小技巧

    #Exchange安装必须开启防火墙服务 #批量建用户: for /f  "tokens=1,2,3,4,5,6,7 delims=," %a in (c:\users.csv) ...

  4. 安卓平台多个视频叠加演示demo说明

    多个音视频编辑演示说明: 第一个-----字幕和视频的叠加: 说明: 把字幕文件中的文字,按照时间叠加到视频上去,形成新的视频. 类似我们看电影时的字幕. 下载地址:http://www.cnblog ...

  5. Qt 学习笔记

    继承自QObject 的Qt类都具有支持信号和槽的能力,并且在子类的实现代码中直接使用connect()函数 pwdLineEdit->setEchoMode(QLineEdit::Passwo ...

  6. CentOS7中将Mysql添加为系统服务

    如果是自己通过tar包安装的Mysql,不会自动添加到系统服务中,可通过如下方式,自己添加. 先启动一下mysql ${mysql}/support-files/mysql.server start ...

  7. 使用android的mediaplayer做成 一个demo,欢迎测试使用

    附件是为一个定制视频产品而简单的写了一个demo,用来说明android的mediaplayer是如何使用的. http://files.cnblogs.com/guobaPlayer/palyerD ...

  8. 《CSS网站布局实录》读书笔记

    从Web标准.HTML标记.CSS语法基础介绍到实用技巧,事无巨细.实体书已不印刷,只能下载电子版 书的背景: 国内第一本web标准的CSS布局书,2006年9月第一版,作者李超. 环境背景: 当时主 ...

  9. 9、JavaScript常用函数

    1.alert()函数 用于弹出消息对话框提示用户信息,消息对话框由系统提供,不同浏览器中字体样式可能不同,通常用于调试程序. 2.confirm()函数 弹出一个OK按钮和一个Cancel按钮的消息 ...

  10. java全组合算法

    public static void combination(int[] s) { if (s.length == 0) { return; } int len = s.length; int n = ...