首页 > 代码库 > hdu1003

hdu1003

Max Sum

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


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
 

 

Author
Ignatius.L
这题是一个经典dp问题,dp思想在于把问题分解成若干个子问题,在子问题最优的情况下得出最终最优结果,所以我们每一步都是建立在前一步是最优的基础之上的。所以解决dp问题,最基本的要在某种情景下,想到当前状态的最优情况是什么样的,最优后,接下来怎么办,不是最优的该怎么变成最优的。这就是我们的状态转移方程。
对于本问题,首先明确,连续的子段! 和最大 ,一个数组给我们,第一个数肯定当前最大,毋庸置疑,那么遇到下一个数,怎么判断和是当前最大呢?我们遇到正数,那肯定直接加,因为加完肯定比不加大,如果是负数呢?加上去,原来的和肯定变小,但不加怎么办呢?
基于以上问题 可以得出状态方程 dp[i]=d[i-1]+a[i]>a[i]?dp[i-1]+a[i]:a[i]  (dp[i]表示当前i下最大的子段和,a[i]是需要处理的数字)什么意思呢,当前数字加到之前的和上面后,如果大于当前数字,那么就执行加的操作,如果小于当前数字,就把当前和最大值dp[i]设置为a[i],可能有人问为什么要设置为a[i]。。记住,如果a[i]你不加上去,意味着你需要从新开始累加和了,我们要求是连续的子段和!!!
利用dp数组的代码:
 1 #include<iostream> 2 #include<cstdio> 3 #include<cstdlib> 4 //#define LOCAL    5 using namespace std; 6  7  8 int main() 9 {10 #ifdef LOCAL11 freopen("d:datain.txt","r",stdin);12 freopen("d:dataout.txt","w",stdout);13 #endif 14     int n;15     while(scanf("%d",&n)!=EOF)16     {17         int i,m;18         for(i =0 ; i< n;i++)19         {20             scanf("%d",&m);21             int dp[100000],a[100000];22             scanf("%d",&a[0]);23             dp[0] = a[0];  //当前最大24             for(int j = 1; j<m;j++)   //生成了dp状态数组了25             {26                 scanf("%d",&a[j]);27                 if(dp[j-1]+a[j]<a[j])      //状态转移方程28                     dp[j]=a[j];29                 else30                     dp[j]=dp[j-1]+a[j];31             }32             int Max,End;33             Max = dp[0];34             End = 0;35             for(int j = 1 ;j<m;j++)           //寻找区间36                 if(Max<dp[j])37                 {38                     End = j;39                     Max = dp[j];40                 }41             int Begin = End;42             int temp = 0;43             for(int j = End;j>=0;j--)44             {45                 temp +=a[j];46                 if(temp==dp[End])47                     Begin = j;48             }49             cout<<"Case "<<i+1<<":"<<endl<<Max<<" "<<Begin+1<<" "<<End+1<<endl;50             if(i<n-1)51                 cout<<endl;52         }53     }54     return 0;55 }

简化后不带dp数组的,因为这题在dp问题中是比较简单的。

 1 //hdu 1003 2  3 #include<stdio.h> 4 int main() 5 { 6  7     int n; 8     while(scanf("%d",&n)!=EOF) 9     {10         for(int i = 0;i<n;i++)11         {12             int a;13             int Max  = -9999;14             int sum  = 0,m;15             int Begin=0,End=0,flag=0;16             scanf("%d",&m);17             scanf("%d",&a);18             Max = sum = a;19             for(int j = 1 ;j<m ;j++)20             {21                 scanf("%d",&a);22                 if(sum<0)23                 {24                     sum=a;25                     flag=j;26                 }27                 else28                 {29 30                     sum=sum+a;31                 }32                 if(Max<sum)33                 {34                     Max = sum ;35                     Begin =flag;36                     End = j;37                 }38             }39             printf("Case %d:\n%d %d %d\n",i+1,Max,Begin+1,End+1);40             if(i<n-1)41                 printf("\n");42         }43 44     }45     return 0;46 }