HDU 1024 Max sum plus (dynamic planning, given an array, divides it into m non-Intersecting sub-segments and the maximum value)

Source: Internet
Author: User
Max sum plus

Time Limit: 2000/1000 MS (Java/others) memory limit: 65536/32768 K (Java/Others)
Total submission (s): 6725 accepted submission (s): 2251

Problem descriptionnow I think you have got an AC in Ignatius. l's "Max sum" problem. to be a brave acmer, we always challenge ourselves to more difficult problems. now you are faced with a more difficult problem.

Given a consecutive number sequence s1, S2, S3, S4... SX,... SN(1 ≤ x ≤ n ≤ 1,000,000,-32768 ≤ SX≤ 32767). We define a function Sum (I, j) = sI+... + SJ(1 ≤ I ≤ j ≤ n ).

Now given an integer m (M> 0), your task is to find m pairs of I and j which make sum (I1, J1) + Sum (I2, J2) + Sum (I3, J3) +... + Sum (IM, JM) Maximal (IX≤IY≤ JXOr IX≤ JY≤ JXIs not allowed ).

But I'm lazy, I don't want to write a special-Judge module, so you don't have to output m pairs of I and J, just output the maximal summation of sum (IX, JX) (1 ≤ x ≤ m) instead. ^_^

Inputeach test case will begin with two integers m and n, followed by N integers s 1 , S 2 , S 3 ... S N .
Process to the end of file.

Outputoutput the maximal summation described abve in one line.

Sample input1 3 1 2 3
2 6-1 4-2 3-2 3

Sample output6
8

Hint

Huge input, scanf and dynamic programming is recommended.

 

Authorjgshining (Aurora shadow)

The big think of this question is to divide an arrayMNumber of non-Intersecting child segments and maximum values.

set num for a given array, n indicates the total number of elements in the array, status [I] [J] indicates the front I select the number I count j maximum value of a segment, 1 <= j <= I <= N & J <= m , the state transition equation is:

status [I] [J] = max (status [I-1] [J] + num [I] , MAX (status [0] J-1] ~ Status [I-1] [J-1]) + num [I])

At first glance, this equation is quite scary, becauseNIs1 ~ 1,000,000WhileMThe specified range is not provided,MThe memory will burst as long as it is a little bigger. However, after careful analysis, you will find thatStatus [I] [J]OnlyStatus [*] [J]AndStatus [*] [J-1]So this question only requires two one-dimensional arrays to complete state transfer.

Further analysis shows thatMax (status [0] [J-1] ~ Status [I-1] [J-1])You do not need to obtain it separately. ObtainNow_status(Save the status of the array) in the processPre_status(Save the array of the previous status) for synchronous update.

 

 

Status DP [I] [J] Has the number of first J, the maximum value of the sum that constitutes the I group. Decision: indicates the number of J groups, including I groups, or independent groups. Equation DP [I] [J] = max (DP [I] [J-1] + A [J], max (DP [I-1] [k]) + A [J]) 0 <k <j Space complexity, M unknown,N <= 1000000, Continue to scroll the array.

Time Complexity n ^ 3. n <= 1000000. Obviously, the optimization will continue. Max (DP [I-1] [k])Is the maximum value of the previous 0... J-1. We can record the first J items each time we calculate DP [I] [J ]. The maximum value is saved in an array and can be used in the next calculation, so that the time complexity is n ^ 2.

 /* 
Status DP [I] [J] has the number of first J, which is the maximum value of the sum of the I groups. Decision Making:
Whether the number of J is contained in group I or the group is independent.
Equation DP [I] [J] = max (DP [I] [J-1] + A [J], max (DP [I-1] [k]) + A [J]) 0 <k <j
Space complexity, M unknown, n <= 1000000, continue to scroll the array.
Time Complexity n ^ 3. n <= 1000000. Obviously, the optimization will continue.
Max (DP [I-1] [k]) is the maximum value of the previous 0... J-1.
We can record the maximum values of the first J when calculating DP [I] [J ].
You can use the array to save it for the next computation. the time complexity is n ^ 2.
*/

# Include < Stdio. h >
# Include < Algorithm >
# Include < Iostream >
Using Namespace STD;
# Define Maxn 1000000
# Define INF 0x7fffffff
Int DP [maxn + 10 ];
Int MMax [maxn + 10 ];
Int A [maxn + 10 ];
Int Main ()
{
Int N, m;
Int I, j, mmmax;
While (Scanf ( " % D " , & M, & N) ! = EOF)
{
For (I = 1 ; I <= N; I ++ )
{
Scanf ( " % D " , & A [I]);
MMax [I] = 0 ;
DP [I] = 0 ;
}
DP [ 0 ] = 0 ;
MMax [ 0 ] = 0 ;
For (I = 1 ; I <= M; I ++ )
{
Mmmax =- INF;
For (J = I; j <= N; j ++ )
{
DP [J] = Max (DP [J - 1 ] + A [J], mMax [J - 1 ] + A [J]);
MMax [J - 1 ] = Mmmax;
Mmmax = Max (mmmax, DP [J]);
}
}
Printf ( " % D \ n " , Mmmax );

}
Return 0 ;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.