Max Sum plus plus---hdu1024 (dynamic programming for maximum sum of M segments)

Source: Internet
Author: User

Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=1024

Test instructions is the number of n is divided into M-segment, the largest and;

DP[I][J] means that the J number is divided into the I segment, the result of selecting the number of J, rather than the current optimal solution,

So consider whether the number of numbers I have is a paragraph or a paragraph from the front.

So Dp[i][j]=max (Dp[i][j-1]+a[j], max+a[j]); Where Max divides the first j-1 number into the maximum value in the I-1 segment;

Because the problem of N is 100w,m do not know how much, so open a two-dimensional array may not be because only each state is related to its previous state, so we can use the DP "2" "N" to implement the scrolling array;

Xor ^ is the same as 0, the difference is 1, you can use ^1 to convert the state;

The problem of boundary should be considered;

#include <cstdio>#include<cstring>#include<iostream>#include<cmath>#include<vector>#include<algorithm>using namespacestd;#defineN 1000050#defineMOD 1000000007#defineMet (A, b) memset (A, B, sizeof (a))#defineINF 0x3f3f3f3ftypedefLong LongLL;intN, M, dp[2][n], a[n];intMain () { while(SCANF ("%d%d", &m, &n)! =EOF) {Met (DP,0); Met (A,0);  for(intI=1; i<=n; i++) scanf ("%d", &A[i]); intMax, ans, k; K=0;  for(intI=1; i<=m; i++) {k= k^1; Dp[k][i]= dp[k^1][i-1]+a[i];///Select the number of I, divided into paragraph I, so can only be a paragraph, then can only write;Max= dp[k^1][i-1]; Ans=Dp[k][i];  for(intj=i+1; j<=n; J + +)///J to start from I+1 because: to be divided into segments I must have at least I number, there is a j-1 below;{Max= Max (max, dp[k^1][j-1]);///Max divides the number of previous j-1 into the maximum value in the i-1 segment;Dp[k][j]= MAX (max + a[j], dp[k][j-1] + a[j]);///himself into a paragraph (Max); and the preceding paragraph (dp[i][j-1]);ans=Max (Dp[k][j], ans); }} printf ("%d\n", ans); }    return 0;}
View Code

Max Sum plus plus---hdu1024 (dynamic programming for maximum sum of M segments)

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.