the algorithm of three version below is essentially the same, namely, Kadane’s algorithm, which is of O(n) complexity. https://en.wikipedia.org/wiki/Maximum_subarray_problem

the evolution of the implementations is to remove redundancy and do what is really needed, the side effect of doing so is becoming more efficient.

IMHO, version 1.0.2 is well commented which shows the guidelines of efficient bookkeeping of boundaries, first and last can also be easily changed to first_iter and last_iter to print the whole subarray if needed.

// version 1.0.0, a coordinate array, a traversing to find the first element

#include <cstdio>
#include <algorithm> #define MAXSIZE 10005 int main() {
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif
int N,i,tmp,*p;
int nums[MAXSIZE]={-1}, dp[MAXSIZE]={0};
while(scanf("%d",&N)==1 && N>0) {
for(i=1;i<=N;++i) { scanf("%d",&nums[i]); }
for(i=1;i<=N;++i) {
tmp=nums[i]+(dp[i-1]>0?dp[i-1]:0);
dp[i]=tmp>0?tmp:0;
//dp[i]=std:max(0,nums[i]+std::max(dp[i-1],0));
}
p=std::max_element(dp,dp+N+1);
if(p==dp) {
if(nums==std::max_element(nums,nums+N+1)) { i=1,tmp=N; }
else {
for(i=0;i<=N && nums[++i]<0;) {}
for(tmp=i;nums[++tmp]==0;) {} --tmp;
}
}
else {
for(tmp=i=p-dp;i>0 && dp[--i]>0;) {}
for(;i>0 && nums[i]==0 && dp[--i]==0;) {} ++i;
}
printf("%d %d %d\n",*p,nums[i],nums[tmp]);
}
return 0;
}

// version 1.0.1, no coordinate array, modifying the data, a traversing to find first element

#include <cstdio>
#include <algorithm> #define MAXSIZE 10005 int main() {
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif
int N,i,j,tmp,last,sum;
int nums[MAXSIZE]={-1};
while(scanf("%d",&N)==1 && N>0) {
for(i=1;i<=N;++i) { scanf("%d",&nums[i]); }
for(sum=-1,last=nums[N],j=0, i=1;i<=N;++i) {
tmp=nums[i]+(nums[i-1]>0?nums[i-1]:0);
if(tmp>=0) {
if(tmp>sum) { sum=tmp; last=nums[i]; j=i; }
nums[i]=tmp;
}
}
if(sum==-1) ++sum;
else for(;j>0 && nums[--j]>=0;) {}
printf("%d %d %d\n",sum,nums[j+1],last);
}
return 0;
}

// version 1.0.2, no coordinate array, not modify data, no extra traversing to find boundary element

#include <cstdio>
#include <algorithm> #define MAXSIZE 10005 int main() {
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif
// prev -- maxsum ending here, sum -- maxsum so far, res -- result
int N,i,first,last,tmp,sum,res,prev;
int nums[MAXSIZE];
while(scanf("%d",&N)==1 && N>0) {
for(i=0;i<N;++i) { scanf("%d",&nums[i]); }
for(res=prev=sum=-1,first=nums[0],last=nums[N-1], i=0;i<N;++i) {
if(prev<0) {
if(nums[i]>=0) {
// prev start increasing, update candidate of first -- tmp
tmp=prev=nums[i];
// update candidate of result -- sum
if(prev>sum) { sum=prev; }
}
}
else {
prev+=nums[i];
// prev stop increasing, update first, last, res
if(nums[i]<=0) { if(sum>res) { res=sum; first=tmp; last=nums[i-1]; } }
// update candidate of result -- sum
else if(prev>sum) { sum=prev; }
}
}
// update first, last, res, -- only if partial sum remain increasing
if(sum>res) { res=sum; first=tmp; last=nums[i-1]; }
// all negative
if(res==-1) ++res;
printf("%d %d %d\n",res,first,last);
}
return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。// p.s. If in any way improment can be achieved, better performance or whatever, it will be well-appreciated to let me know, thanks in advance.

hdu 1231, dp ,maximum consecutive sum of integers, find the boundaries, possibly all negative, C++ 分类: hdoj 2015-07-12 03:24 87人阅读 评论(0) 收藏的更多相关文章

  1. Hiking 分类: 比赛 HDU 函数 2015-08-09 21:24 3人阅读 评论(0) 收藏

    Hiking Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) Total Subm ...

  2. HDU 1532 Drainage Ditches 分类: Brush Mode 2014-07-31 10:38 82人阅读 评论(0) 收藏

    Drainage Ditches Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  3. Hdu 1507 Uncle Tom's Inherited Land* 分类: Brush Mode 2014-07-30 09:28 112人阅读 评论(0) 收藏

    Uncle Tom's Inherited Land* Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (J ...

  4. hdu 1503, LCS variants, find a LCS, not just the length, backtrack to find LCS, no extra markup 分类: hdoj 2015-07-18 16:24 139人阅读 评论(0) 收藏

    a typical variant of LCS algo. the key point here is, the dp[][] array contains enough message to de ...

  5. Hdu 1506 Largest Rectangle in a Histogram 分类: Brush Mode 2014-10-28 19:16 93人阅读 评论(0) 收藏

    Largest Rectangle in a Histogram Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 ...

  6. Hdu 1010 Tempter of the Bone 分类: Translation Mode 2014-08-04 16:11 82人阅读 评论(0) 收藏

    Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Othe ...

  7. hdu 1082, stack emulation, and how to remove redundancy 分类: hdoj 2015-07-16 02:24 86人阅读 评论(0) 收藏

    use fgets, and remove the potential '\n' in the string's last postion. (main point) remove redundanc ...

  8. Hdu 1429 胜利大逃亡(续) 分类: Brush Mode 2014-08-07 17:01 92人阅读 评论(0) 收藏

    胜利大逃亡(续) Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Subm ...

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

随机推荐

  1. Webpack、Browserify和Gulp

    https://www.zhihu.com/question/37020798 https://www.zhihu.com/question/35479764

  2. Maven 打成 Webjar的方法

    http://blog.csdn.net/sxdtzhaoxinguo/article/details/45666789 *************************************** ...

  3. SSH邮箱验证与激活

    下面是我写的email验证和激活: 自己瞎写的,能用,不喜欢勿喷 action中regist方法中代码 /** * * 发送邮件的方法 */ StringBuffer sb=new StringBuf ...

  4. [转]Android开源项目第二篇——工具库篇

    本文为那些不错的Android开源项目第二篇--开发工具库篇,主要介绍常用的开发库,包括依赖注入框架.图片缓存.网络相关.数据库ORM建模.Android公共库.Android 高版本向低版本兼容.多 ...

  5. 采用MANIFEST.MF之jar报错ClassNotFoundException解法

    检查n多遍也试了n多次,证明下面是MANIFEST.MF文件正确写法: Manifest-Version: 1.0 Premain-Class: cn.yandz.monitor.SizeOfObje ...

  6. iOS TextField用法大全

    //初始化textfield并设置位置及大小 UITextField *text = [[UITextField alloc]initWithFrame:CGRectMake(20, 20, 130, ...

  7. Matrix

    记载: Matrix Matrix是Android 提供的一个矩阵工具类,位于"android.graphics.Matrix"包下,它本身不能对图像或View进行变换, 但它可以 ...

  8. DataTable筛选出现异常

    异常详细信息: System.ArgumentException: 在 Range 对象中,Min (37)必须小于或等于 max (-1). 解决方法:转化字段类型为int 再次出现问题:HH> ...

  9. 005_kafka_Java_API

    1.生产者Producer 1)添加依赖 <dependency> <groupId>org.apache.kafka</groupId> <artifact ...

  10. csshover.htc CSS兼容

    以下为csshover.htc 内容 <attach event="ondocumentready" handler="parseStylesheets" ...