HDU Max Sum plus Plus (recursive)

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): 18653 Accepted Submission (s): 6129


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 INPUT1 3 1 2 32 6-1 4-2 3-2 3

Sample Output68 HintHuge input, scanf and dynamic programming is recommended.

Dp[i][j][0] ... Indicates that the number of first I is divided into J groups, and the maximum score of the number of No. i is not selected

DP[I][J][1] ... Indicates the number of first I is divided into J groups, the maximum score of the number of selected I

Because state i is only i-1 with the state, you can use a scrolling array to reduce space

Take the most to write yourself. Otherwise the card constant will time out

#include <iostream>#include<cstring>#include<cstdio>#include<algorithm>using namespacestd;Const intN =100010;Const intINF = 1e9+7;intdp[2][n][2], N, M, x[n]; inlineintMAX (intAintb) {if(A > B)returnA; Else returnb;}intMain () {//freopen ("In.txt", "R", stdin);     while(~SCANF ("%d%d",&m,&N)) { for(inti =1; I <= N; ++i) {scanf ("%d",&X[i]); }        intv =0 ; dp[v][0][0] =0 ; dp[v][1][1] = x[1] ;  for(inti =1; I < n; ++i) { for(intj =0; J <= i +1&& J <= M; J + +) {Dp[v^1][j][0] = dp[v^1][j][1] = -inf; }             for(intj = min (M, i); J >=0; --j) {if(J! =i) {dp[v^1][j+1][1] = MAX (dp[v][j][0] + x[i+1], dp[v^1][j+1][1] ); Dp[v^1][j][0] = MAX (dp[v][j][0], dp[v^1][j][0]); }                if(J! =0) {Dp[v^1][j][1] = MAX (dp[v^1][j][1], dp[v][j][1] + x[i+1] ) ; Dp[v^1][j+1][1] = MAX (dp[v^1][j+1][1], dp[v][j][1] + x[i+1] ) ; Dp[v^1][j][0] = MAX (dp[v^1][j][0], dp[v][j][1] ) ; }} v^=1 ; }        intAns =-inf; if(M < n) ans = MAX (ans, dp[v][m][0] ); if(M >0) ans = MAX (ans, dp[v][m][1] ); printf ("%d\n", ans); }    return 0 ;}
View Code

HDU Max Sum plus Plus (recursive)

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.