hdu 1231, dp ,maximum consecutive sum of integers, find the boundaries, possibly all negative, C++
2015-07-12 03:24
661 查看
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
// version 1.0.1, no coordinate array, modifying the data, a traversing to find first element
// version 1.0.2, no coordinate array, not modify data, no extra traversing to find boundary element
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
[code]#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
[code]#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 ,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
[code]#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; }
相关文章推荐
- sizeof,终极无惑『转』
- KeyPoint类型者转化为c++普通类型
- OpenGL蓝宝书第五章代码勘误以及惯性坐标系去解释模型变换:Pyramid.cpp
- Lowest Common Ancestor of a Binary Search Tree
- [C++]GCC的C++入门(2012-10-28更新)
- 【C++基础】——拷贝构造函数的浅拷贝和深拷贝
- 个人c语言感悟
- stl非变易算法(一)
- C++暂停黑窗口 system( “pause “);(getch(),getchar(),system(pause)的联系和区别!!!)
- 读陈浩的《C语言结构体里的成员数组和指针》总结,零长度数组
- How to determine the size of a class/struct in the C++?
- C++输入cin详解
- [leetcode-]Climbing Stairs(C语言)
- extended initializer lists only available with -std=c++11
- C++链表模板类
- C++链表模板类
- 字符串比较 c语言
- C语言实现IP地址字符串转化成数值地址(双字)
- c风格字符串与c++string的比较
- 一元线性回归模型与最小二乘法及其C++实现