Hangzhou Electric (Max Sum plus plus)

Source: Internet
Author: User

Click to open Hangzhou Electric 1024

Problem Descriptionnow I Think you have got a 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 S 1, S 2, S 3, S 4... S x, ... S N(1≤x≤n≤1,000,000, -32768≤s x≤32767). We define a function sum (i, j) = S I+ ... + S J(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 (i 1J 1) + SUM (i 2J 2) + SUM (i 3J 3) + ... + sum (i mJ m) Maximal (i x≤i y≤j xOr I x≤j y≤j xis 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 (i xJ x) (1≤x≤m) instead. ^_^

Inputeach test case would begin with a 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 above in one line.

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

Sample Output
68
Ideas:

Classic Dynamic Programming Optimization problem: Set F (i, j) to indicate that the first I number is divided into J, and includes the number of the largest m sub-segment and, then there is the DP equation:
F (i, j) = max {f (i-1, J) + V[i], max {f (k, j-1) + V[i]} (k = j-1 ... i-1)}. You can introduce a secondary array to optimize the transfer. Set g (I, j) to indicate that the number of first I is divided into the maximum sub-segment of J and (note that the number of I may not be in the J-segment), then the recursive relationship is as follows: G (i, j) = Max{g (I-1, J), F (i, j)}, whether or not the number of I is added to transfer such a recursive relationship of F will become: f (i, J) = Max{f (I-1, J), G (I-1, J-1)} + v[i], so the final result is g[n][m], which cleverly optimizes the transfer by introducing an auxiliary array. The implementation can use a one-dimensional array, speed quickly g[i][j] or g[i-1][j] equal, or f[i][j] equal, f[i][j]-a[i] or g[i-1][j-1] equal, or f[i-1][j] equal. Turn into a one-dimensional array to the first row: f[j]=max{f[j],g[j-1]}+a[i]} g[j]=max{g[j],f[j]}.


Code implementation:

Import java.util.*;class main{public    static void Main (string[] args) {        int J;        Scanner sc=new Scanner (system.in);        while (Sc.hasnext ()) {            int m=sc.nextint (); int n=sc.nextint ();            Int[] a=new int[n+1];int[] dp=new int[m+1];int[] dp2=new int[m+1];            for (int i=1;i<=n;i++) {                a[i]=sc.nextint ();            }            DP[1]=A[1];DP 2[1]=a[1];            for (int i=2;i<=n;i++) {                for (j=1;j<=math.min (I, M); j + +) {                    Dp2[j]=math.max (dp2[j]+a[i], dp[j-1]+a[i]) ;                    Dp[j-1]=math.max (Dp[j-1], dp2[j-1]);                }                Dp[j-1]=math.max (Dp[j-1], dp2[j-1]);            }            System.out.println (Dp[m]);}}    




Hangzhou Electric (Max Sum plus plus)

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.