HDU 1024 M子段和最大
M子段和最大问题
Given an array and divide into m sequences. One sequences at least has one number and the sequences can’t contain the number with same index. And the index of the numbers in one sequence should be continuous. Notice that we don’t have to use all the numbers.
N between 1 to 1e6.
SOLUTION
Dynamic programming
.
Let d[i][j]
denote the maximum sum of the current array that is divided into i segments and the last segment ends with the jth number.
When comes to the jth number, we have two options. One is the jth number to be a new segments, the other is to add it to the back of the last segment which ends with (j-1)th number.
So the transfer equation is dp[i][j]=max(pre_max+a[j],dp[i][j-1]+a[j])
. pre_max
represents to the maximum answer that the pre array divided into i-1
segments.
Code
1 |
|