51nod 1052 Max M sub-segment and & POJ 2479 maximum two sub-segments and

Source: Internet
Author: User

extension of the maximum sub-segment and classic problem. title Link: http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1052 n integers consisting of a sequence of a[1],a[2],a[3],..., A[n], dividing the n number into disjoint m-sub-segments, and the largest of the M segments. If M >= the number of positive numbers in N, then the sum of all positive numbers is output. For example:-2 11-4 13-5 6-2, divided into 2 segment, 11-4 131 segment, 61 segment, and 26. Input
Line 1th: 2 numbers n and m, separated by a space in the middle. n is the number of integers, and M is divided into how many segments. (2 <= N, M <= 5000) line 2-n+1: N integers ( -10^9 <= a[i] <= 10^9)
Output
Output this most Yamato
Input example
7 2-211-413-56-2
Output example
26
use two arrays, PRE[MAXN] and DP[MAXN]. first the M-cycle, the X-cycle represents the maximum X sub-segments that can be obtained by dividing the entire sequence into X-segments. The Pre[i] Array records the maximum number of sub-segments that can be obtained from the 1th count to the number of I to be divided into X segments. assume that you are currently partitioned into X sub-segments (that is, the outermost loop executes x times), and then you need to perform the first x+1:The Dp[i] transfer is the max value from the following two scenarios. 1. The number of the former i-1 is divided into X sub-paragraphs to obtain the maximum and pre[i-1] plus a separate input[i] as the beginning of the paragraph x+1 sub-paragraph;2. The x+1 sub-paragraph has been separated from the number of i-1, and the tail of the X+1 sub-paragraph needs to be added input[i], i.e. dp[i-1]+input[i] AC Code:
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include < Limits.h>using namespace Std;typedef long long LL; #define MAXN 5010const ll min_inf =-(1 << 30);    LL DP[MAXN], INPUT[MAXN], Pre[maxn];int N, m;int Main () {scanf ("%d%d", &n, &m);    Dp[0] = Min_inf;    Pre[0] = 0;        for (int i = 1; I <= n; i++) {scanf ("%i64d", &input[i]);        Pre[i] = 0;    Dp[i] = Min_inf;    } LL ans = min_inf;        while (m--) {ans = min_inf;            for (int i = 1; I <= n; i++) {if (i = = 1) dp[i] = Pre[i-1] + input[i];                else {if (dp[i-1] > Pre[i-1]) {dp[i] = Dp[i-1] + input[i];//The number of I and its previous number within the same subsection            } else {Dp[i] = Pre[i-1] + input[i];//A new sub-segment from I}} Dp[i] = max (dp[i-1], pre[i-1]) + input[i];            Or the direct value can be pre[i-1] = ans; ans = max (ans, dp[i));    } Pre[n] = ans;    } printf ("%i64d\n", Pre[n]); return 0;}

  

Similarly, m=2 can also solve the problem of poj2479. http://poj.org/problem?id=2479

51nod 1052 Max M sub-segment and & POJ 2479 maximum two sub-segments and

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.