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 <=)
line 2-n+1: N integers ( -10^9 <= a[i] <= 10^9)
Output
Output this most Yamato
Input example
7 2
-2
-
4-
5
6
-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. Assuming that the current is divided into X sub-segments (that is, the outermost loop executes x times), and then you need to perform the x+1: Then the transfer of Dp[i] takes the max value from the following two cases. 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 paragraph x+1; 2. I-1 Sub-paragraphs have been separated from the previous x+1 number, 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 5010 Const 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];//number of I in the same subsection as its previous number
            Inside} 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 you can just take the value 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


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.