Hdu1024_max Sum plus plus "scrolling array"

Source: Internet
Author: User

Max Sum plus plus

Time limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)Total Submission (s): 17436 Accepted Submission (s): 5731
Problem Description
Now I think you has got an AC in IGNATIUS.L ' s "Max Sum" problem. To is a brave acmer, we always challenge ourselves to more difficult problems. Now you is 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 a 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≤JX or IX≤JY≤JX is not allowed).


But I ' m lazy, I don't want to the write a Special-judge module, so you don ' t has to output m pairs of I and j, just output th e maximal summation of sum (ix, JX) (1≤x≤m) instead. ^_^

Input
Each test case would begin with a integers m and n, followed by n integers S1, S2, S3 ... Sn.
Process to the end of file.

Output
Output the maximal summation described above in one line.

Sample Input
1 3 1) 2 3
2 6-1 4-2) 3-2 3

Sample Output
6
8

Hint


Huge input, scanf and dynamic programming is recommended.


Author

Jgshining (Aurora Dazzle)


To give you two numbers m and N, followed by n number, from the n number to find M sub-segment,

Find the largest and most large M-segments

Idea: At first, I didn't know how to find the state transfer equation. Refer to other people's blogs to understand.

. set Dp[i][j] to divide the first J number into the maximum and of the I segment. NUM[J] is the current number

Then the transfer equation is dp[i][j] = max (Dp[i][j-1]+num[j],dp[i-1][k]+num[j]) (i-1<=k<=j-1)

can also be treated as dp[i][j] = max (Dp[i][j-1]+num[j],max(Dp[i-1][i-1],dp[i-1][i],..., dp[i-1][j-1]))

Note: Max (Dp[i-1][i-1],dp[i-1][i],..., dp[i-1][j-1]) is dp[i-1][j-1]

Meaning: The first J number is divided into the largest of the I segment and has two decisions.

1. Incorporating the current J-number into paragraph I, with the number of j-1 paragraph and for a period of the largest and.

2, the current number J as paragraph I , and the number of the section k is in paragraph i-1, interval (k+1,j-1)

no longer select the maximum and .

The maximum value of the two decisions.

Another difficulty in this subject is to convert the two dimensions to an array. Taking into account that the state of line I is being handed over by line I-1 and line I,

So you can use a scrolling array to compress the two-dimensional array into one-dimensional arrays, the process is a bit less understanding, to stay in the future to think slowly.

Reference post: http://blog.csdn.net/acm_davidcn/article/details/5887401

#include <stdio.h> #include <string.h> #include <algorithm>using namespace Std;int dp[1000010];int Maxn[1000010];int Num[1000010];int Main () {    int m,n;    while (~SCANF ("%d%d", &m,&n))    {        dp[0] = maxn[0] = 0;        for (int i = 1; I <= N; i++)        {            scanf ("%d", &num[i]);            Dp[i] = Maxn[i] = 0;        }        int MAXN;        for (int i = 1; I <= M; i++)//divided into I segment        {            MAXN = -0xffffff0;            for (int j = i; J <= N; j + +)//J number            {                dp[j] = max (dp[j-1]+num[j],maxn[j-1]+num[j]);                Maxn[j-1] = MAXN;                MAXN = max (Maxn,dp[j]);            }        }        printf ("%d\n", MAXN);    }    return 0;}



Hdu1024_max Sum plus plus "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.