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. redis的持久化 rdb和aof

    1.rdb(Redis DataBase) 当满足条件时,redis单独会fork(创建)一个新的线程,会先将内存中的数据写入到一个临时文件中,待持久化过程都结束了,再用这个临时文件替换上次已经持久化 ...

  2. android studio 程序错误

    一.错误类型: com.android.tools.fd.runtime.BootstrapApplication cannot be cast to 成功修改方式 File --> Setti ...

  3. 必须掌握的Linux命令

    章节简述: 本章节讲述系统内核.Bash解释器的关系与作用,教给读者如何正确的执行Linux命令以及常见排错方法. 经验丰富的运维人员可以恰当的组合命令与参数,使Linux字符命令更加的灵活且相对减少 ...

  4. NGINX----源码阅读----init配置脚本

    /auto/init init脚本负责初始化各种目录环境变量值. 1.make文件.源文件.头文件.配置头文件路径变量初始化. NGX_MAKEFILE=$NGX_OBJS/Makefile NGX_ ...

  5. Another Array of Orz Pandas

    Another Array of Orz Pandas 题目链接:http://acm.xidian.edu.cn/problem.php?id=1187 线段树 线段树维护区间和以及区间内各个数平方 ...

  6. git上传报错的解决方案

    刚创建的github版本库,在push代码时出错:$ git push -u origin masterTo git@github.com:******/Demo.git ! [rejected] m ...

  7. JS正则表达式的test()方法检查汉字

    汉字的检查: var reg1=/[^\u4e00-\u9fa5]/g; var text="正则表达式1"; var res1=reg1.test(text);//为true表示 ...

  8. 前端学习 html

    Html 前端学习 <!DOCTYPE html> < lang="en"> <head> <meta charset="UTF ...

  9. JavaFX基础学习之OkHttp/Gson2

    222 package application; import java.io.IOException; import java.net.URL; import java.util.ResourceB ...

  10. 12c windows auto installl

    --config for oralce_envset ORACLE_HOME=c:\app\oracle\product\12.1.0\dbhome_1set ORACLE_SID=testuatse ...