本文主要是介绍HDU_Max Sum(DP),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
解题报告
http://blog.csdn.net/juncoder/article/details/38150533
题目传送门
题意:
求子区间连续和最大
思路:
DP,dp[i]=max(dp[i-1]+num[i],num[i])
如果区间内有一个数使得连续和小于0,那么从那个数开始重新定位区间。
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
int main()
{int t,i,j,n,num[100100],dp[100100],k=1;cin>>t;while(t--){cin>>n;for(i=1;i<=n;i++)cin>>num[i];int maxx=-10000,s=1,e=1,sum=0,a=1,b=1;for(i=1;i<=n;i++){if(sum<=0){sum=num[i];a=b=i;}else{sum+=num[i];b=i;}if(sum>maxx){maxx=sum;s=a;e=b;}}printf("Case %d:\n",k++);printf("%d %d %d\n",maxx,s,e);if(t)printf("\n");}
}
Max Sum
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 142391 Accepted Submission(s): 33136
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
2 5 6 -1 5 4 -7 7 0 6 -1 1 -6 7 -5
Sample Output
Case 1: 14 1 4Case 2: 7 1 6
这篇关于HDU_Max Sum(DP)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!