hdu 1003 MaxSum

来源:互联网 发布:java lambda 接口 编辑:程序博客网 时间:2024/05/13 20:47

tips: 最大子序列和问题.算法经典例题.....当初学数据结构和算法分析的时候没有实现过,今天被实验室的学弟问到这个题,特意做了一遍。


算法书上和杭电的这道题的最大区别在于:

1.如果数据全部是负数,算法书上认为序列长度为0(最大和自然也为0),而杭电的这道题认为最大的和为最大的那一个负数。

2.比书上稍微复杂一点,要求输出开始和结束序列的位置

先贴杭电的题目以及代码(简单DP),再贴书上的代码。


Max Sum

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 219609    Accepted Submission(s): 51843


Problem Description
Given a sequence a[1],a[2],a[3]......a[n], your job is to calculate the max sum of a sub-sequence. For example, given (6,-1,5,4,-7), the max sum in this sequence is 6 + (-1) + 5 + 4 = 14.
 

Input
The first line of the input contains an integer T(1<=T<=20) which means the number of test cases. Then T lines follow, each line starts with a number N(1<=N<=100000), then N integers followed(all the integers are between -1000 and 1000).
 

Output
For each test case, you should output two lines. The first line is "Case #:", # means the number of the test case. The second line contains three integers, the Max Sum in the sequence, the start position of the sub-sequence, the end position of the sub-sequence. If there are more than one result, output the first one. Output a blank line between two cases.
 

Sample Input
25 6 -1 5 4 -77 0 6 -1 1 -6 7 -5
 

Sample Output
Case 1:14 1 4Case 2:7 1 6
 
tips:中途提交了下,有两个坑,第一个是我把ans赋值为一负数,导致程序出错(应该为0).2.最后一次输出只需加一个换行符

#include<iostream>using namespace std;typedef long long ll;ll ans;int s,e,s1;int a[100003];int cnt;int size;int flag;void fum(){ll sum=0;for(int i=1;i<=size;i++){sum+=a[i];if(sum>ans){ans=sum;e=i;s=s1+1;}else if(sum<0){sum=0;s1=i;}}}int main(){int t;cin>>t;while(t--){cnt++;ans=0;s=e=1;s1=flag=0;cin>>size;int max=-99999;int index=1;for(int i=1;i<=size;i++){cin>>a[i];if(a[i]>=0)flag=1; if(a[i]>max){max=a[i];index=i;} }  if(flag) { fum();  cout<<"Case "<<cnt<<":"<<endl<<ans<<" "<<s<<" "<<e<<endl;  } else { cout<<"Case "<<cnt<<":"<<endl<<max<<" "<<index<<" "<<index<<endl;  }  if(t!=0)cout<<endl; }return 0;}


下面的代码,是按照书上的思路写的。


下面贴出一个二分递推算法。

核心思想是,最大和序列,要么在左边,要么在右边,要么跨越了两边(对,我听着也像废话,但代码实现起来很优美)。

对这个算法进行分析:

T(1) = 1 

T(N) = 2T(N/2) + O(N) 

最后得出算法的复杂度为:O(NlogN) 

#include<iostream>using namespace std;typedef long long ll;int a[100003];int cnt;ll MaxSequenceSum(int left,int right){if(left==right){return a[left]>0?a[left]:0;}int center=(left+right)/2;ll leftsum=MaxSequenceSum(left,center);ll rightsum=MaxSequenceSum(center+1,right);ll MaxLeftBorderSum=0;ll LeftBorderSum=0;for(int i=center;i>=left;i--){LeftBorderSum+=a[i];if(LeftBorderSum>MaxLeftBorderSum){MaxLeftBorderSum=LeftBorderSum;} }  ll MaxRightBorderSum=0;ll RightBorderSum=0;for(int i=center+1;i<=right;i++){RightBorderSum+=a[i];if(RightBorderSum>MaxRightBorderSum){MaxRightBorderSum=RightBorderSum;} }  return max(max(leftsum,rightsum),MaxLeftBorderSum+MaxRightBorderSum);  }int main(){int n;cin>>n;while(n--){cnt++;int size;cin>>size;for(int i=0;i<size;i++){cin>>a[i];}ll ans=MaxSequenceSum(0,size-1);cout<<"Case "<<cnt<<":"<<endl<<ans<<endl<<endl;}return 0; } 






0 0
原创粉丝点击