HDU 1003 Max Sum【动态规划求最大子序列和详解 】

时间:2022-05-07
本文章向大家介绍HDU 1003 Max Sum【动态规划求最大子序列和详解 】,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

Max Sum

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

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 4

Case 2:
7 1 6

Author

Ignatius.L

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1003

初来乍到,动态规划也是刚刚接触。刚开始用暴力法,Time limit……

在网上搜了代码。大多是只说是动态规划经典问题、求最大子序列和,然后就是一串代码。最好的就是带了几行注释…没有太多通俗的解释…硬着头皮看了一晚上,终于算是有了眉目想通了。

在这里写下自己对这个动态规划求最大子序列和的理解,通俗一点的解释。(只是个人的理解哦,仅供参考)

这里的求最大子序列和应该是变种了吧,呵呵,还要加上最大子序列的起始和终止位置……只要知道怎么求最大子序列和,那么附加个位置应该不难的。

先来看代码:

 1     #include <iostream>  
 2     using namespace std;  
 3     int main()  
 4     {  
 5         int j,i,k,n,m,t;  
 6         int a[100002];  
 7         scanf("%d",&t);  
 8         for (j=1;j<=t;j++)  
 9         {  
10             scanf("%d",&n);  
11             for (i=0;i<n;i++)  
12             {  
13                 scanf("%d",&a[i]);  
14             }  
15             int sum=0,maxsum=-1001,first =0, last = 0, temp = 1;  
16             for (i=0;i<n;i++)  
17             {  
18                 sum += a[i];  
19                 if (sum > maxsum)  
20                 {  
21                     maxsum = sum;first = temp;last = i+1;  
22                 }  
23                 if (sum < 0)  
24                 {  
25                     sum = 0;temp = i+2;  
26                 }  
27             }  
28       
29             printf("Case %d:n%d %d %dn",j,maxsum,first,last);  
30             if (j!=t)  
31             {  
32                 printf("n");  
33             }  
34         }  
35           
36         return 0;  
37     }  

本想用通俗的话语来解释这个道理,结果发现,通俗了以后就非文字所能描述的好的了,需要各种的手势+纸笔画一阵子,无奈表达能力有限,只好……只好用这样的看似非常严密的数学推理来说明了(囧)

对于整个序列a[n]来说,它的所有子序列有很多很多,但是可以将它们归类。

注意,是以**结尾的子序列,其中肯定是要包含**的了

以a[0]结尾的子序列只有a[0]

以a[1]结尾的子序列有 a[0]a[1]和a[1]

以a[2]结尾的子序列有 a[0]a[1]a[2] / a[1]a[2] / a[2]

……

以a[i]结尾的子序列有a[0]a[1]……a[i-2]a[i-1]a[i]  / a[1]a[2]……a[i-2]a[i-1]a[i] / a[2]a[3]……a[i-2]a[i-1]a[i] / …… /  a[i-1]a[i] / a[i]

所有以a[0] ~a[n]结尾的子序列分组构成了整个序列的所有子序列。

这样,我们只需求以a[0]~a[n]结尾的这些分组的子序列中的每一分组的最大子序列和。然后从n个分组最大子序列和中选出整个序列的最大子序列和。

观察可以发现,0,1,2,……,n结尾的分组中,

maxsum a[0] = a[0]

maxsum a[1] = max( a[0] + a[1] ,a[1])  = max( maxsum a[0] + a[1] ,a[1]) 

maxsum a[2] = max( max ( a[0] + a[1] + a[2],a[1] + a[2] ),a[2]) 

= max(  max( a[0] + a[1] ,a[1]) + a[2] , a[2]) 

= max(  maxsum a[1] + a[2] , a[2])

……

依此类推,可以得出通用的式子。

maxsum a[i] = max( maxsum a[i-1] + a[i],a[i])

用递归……当然,不递归也应该是可以解决的。

我们从maxsum  a[0]开始算起。

以后的每个就是  maxsum a[i-1] + a[i] 和 a[i] 中取大的那个。

程序中判断 前一个的最大子序列和小于零时,将其置为0,然后再加a[i] ,这样不就是和a[i] 一样大的么;前一个的最大子序列和只要大于零,那么再加上a[i] 肯定比 a[i] 要大,这样,带有归零的这个 maxsum a[i-1] + a[i] 就是以表示当前位置结束的子序列的最大和了。

剩下的就是要判断起始和终点位置了。

在循环的过程中,每循环一次就算出一个以当前位置结束的最大子序列和。每次循环中最大的那个保存下来,不就是最终所有最大子序列和中的最大值了么。

其中temp保存的是前一个位置的最大子序列和的开始位置(题目中是从1开始的哦);当 sum > maxsum 时(程序中的条件,与说明时的maxsum不太一样哦)就记录最大值,并保持它的开始位置为temp,终止位置即为当前位置(i +1是因为题目中第一个为1,而不是0);

当最大子序列和小于0时,将 temp = i + 2; 其中 i + 1 表示当前位置(理由如上),i + 2就表示当前位置的下一个位置了。既此最大子序列和为负值,那么下一个的最大子序列和应该是它本身,而不再累加前边的。

程序中就两个if 语句,想要说明白还真不容易。

还有,有人会问,当整个序列全是负数时,还对吗?负数也是成立的,如果全是负数的时候,它就是每次都只取当前值作为最大值了,因为负的跟负的不就越加越小了吗。

因为题目中给出的范围是-1000 ~1000,所以这里初始的maxsum 初始化为-1001 ,只有比所有可能的值都小时才行。maxsum初始化为0;那么当序列全是负数时,得出的最大值将是0……这就wrong了

 总之,只要上一个子序列最大和为正,那么无论当前值的正负,都会与当前的相加,这样以当前值结尾的子序列最大和就会增大。(一个正数 加一个 正数2 或者负数 那么都会比这个正数2 或负数原来要增大,同理,一个负数加任何一个数,都会使这个数减小,因此当前一子序列最大和小于零时,我们就归零它了,相当于是不加任何数,而保留当前位置值本身)


内存优化版:

理解了以上思想后,观察上一个代码我们发现,那个a[10000]基本上就没用啊,保存了一些输入数据,可是那些数据只用了一次就没用了。输入数据的for循环和处理数据的for循环是一模一样的,而且处理数据也只是用到当前输入的数据。

于是,数组也可以省去了,直接将两个循环合并。输入一个数据,直接累加……省下不少空间哦。

 1     #include <iostream>  
 2     using namespace std;  
 3     int main()  
 4     {  
 5         int j,i,k,n,m,t;  
 6         int a;  //不需要数组,只需要一个输入变量
 7         scanf("%d",&t);  
 8         for (j=1;j<=t;j++)  
 9         {  
10             scanf("%d",&n);  
11             int sum=0,maxsum=-1001,first =0, last = 0, temp = 1;  
12             for (i=0;i<n;i++)  
13             {  
14                 scanf("%d",&a); 
15                 sum += a;  
16                 if (sum > maxsum)  
17                 {  
18                     maxsum = sum;first = temp;last = i+1;  
19                 }  
20                 if (sum < 0)  
21                 {  
22                     sum = 0;temp = i+2;  
23                 }  
24             }  
25       //注意格式,我就因为将冒号写到了数的前边而wrong answer,郁闷半天才发现……
26             printf("Case %d:n%d %d %dn",j,maxsum,first,last);  
27             if (j!=t)  
28             {  
29                 printf("n");  
30             }  
31         }  
32           
33         return 0;  
34     }