HDU Max Sum plus plus---dp+ scrolling array

Source: Internet
Author: User

HDU 1024

The main topic: given m and N and N number, the maximum number of m consecutive sub-series of n is required, which requires the sub-sequence not to be handed.

Problem-Solving ideas:<1> dynamic programming, defining state Dp[i][j] The value of the I-satin sequence that represents the number of J before the sequence, where the sub-sequence of I includes A[j],

Max (Dp[m][k]), m<=k<=n is the result of the request

<2> initial state: dp[i][0] = 0, dp[0][j] = 0;

<3> State transfer:

Decision: A[j] to be a sub-segment, or to be followed by a previous sub-paragraph

Equation:

A[J] directly after the preceding sub-paragraph, then dp[i][j] = dp[i][j-1] + a[j];

A[j] himself as a sub-paragraph, first to obtain the largest number of K in front of the i-1 section and, plus a[j],

i.e. Dp[i][j] = max (Dp[i-1][k]) + a[j], where 1=<k<=j-1;

i.e. Dp[i][j] = max (Dp[i][j-1] + a[j], Max (Dp[i-1][k]) + a[j]), where k>=1&&k<=j-1;

<4> Space Complexity Optimization:

This is because n can reach 100 0000 and cannot be dp[n][n], so you need to use a scrolling array to optimize the space.

As you can see from the state transition equation, the update of the state Dp[i][j] with the current line position J only needs to take advantage of the first j-1 state maximum of the previous row

So you need up to two rows of arrays

<5> Time Complexity optimization:

Each time you update dp[i][j], you need to find the maximum value of the previous J-1 state of the previous line, so the complexity will reach O (n^3);

In fact, each time you update the current row's dp[i][j], the maximum value of its corresponding number of first j is saved in Pre[j],

This allows the next row to be updated directly with the pre[j] saved by the current row, instead of searching,

Therefore, time complexity can be optimized to O (n^2)

/*HDU Max Sum plus plus---dp+ scrolling array*/#include<cstdio>#include<cstring>#include<algorithm>using namespacestd;Const intMAXN =1000005;intDP[MAXN], PRE[MAXN];//Pre[j] = dp[i-1][intA[MAXN];intm, N;intMain () {#ifdef _local freopen ("D:\\input.txt","R", stdin);#endif     while(SCANF ("%d%d", &m, &n) = =2){        //input         for(inti =1; I <= N; ++i) {scanf ("%d", A +i); }//For (i)        intMAX; //Defines the precursor of the first state of the first row, at which I is equivalent to 1,dp[1][0]dp[0] =0; //initialize the pre array to be all 0, at which point the pre array should have a row of 0,memset (PRE,0,sizeofpre);  for(inti =1; I <= m; ++i) {            //The inner loop does is to update the state of the current line I, noting that J >= I makes senseMAX= -10e8;//initializes Max to a very small number             for(intj = i; J <= N; ++j) {                /*the PRE[J in the state transition equation] holds the maximum value of the number of first J of the previous line, i.e. pre[j] = max (Dp[i-1][k]), 1<=k<=j                    If A[J] directly belongs to paragraph I, then dp[i][j] = dp[i][j-1] + a[j] here is dp[j] = Dp[j-1]+a[j] If A[J] from a paragraph, you need to find the maximum value of dp[i-1][k], wherein 1<=k<=j-1, that is pre[j-1]*/Dp[j]= Max (Dp[j-1], Pre[j-1]) +A[j]; /*below the pre array and the slightly different above pre[j-1] save is the minimum value of the previous j-1 number of the previous line (line i-1) and the following pre [J-1] After the update is saved is the maximum number of previous j-1 of the current row, update is used for the next row of the DP update, that is, I will be +1 before the use of another note max is the last loop updated value, that is, the current line (i) of the former J 1-digit maximum value*/pre[j-1] = MAX;//update the Pre, save the maximum number of j-1 for the current row, and prepare for the DP update for the next line                if(Dp[j] >MAX) {                    //Max recorded here is the maximum value of the first J bit of the current row//to the next inner Loop J +1, which is why Max can be used to update pre[j-1]MAX = Dp[j];//Max records the maximum value of the front J bits of the current row                }            }        }//For (i)printf"%d\n", MAX); }    return 0;}
View Code

          

          

HDU Max Sum plus plus---dp+ scrolling array

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.