"max m and" dp + scrolling arrays

Source: Internet
Author: User

Title Description

Given the number of n, the number of n is divided into the maximum m sub-segments of the M-segment Disjoint.

Gives an integer sequence of a1,a2,a3,a4,...,ax,...,an, wherein 1≤x≤n≤1,000,000, -32768≤sx≤32767.

We define a function sum (i,j) =ai + ... + Aj (1≤i≤j≤n, and Ai~aj is a sequential number).

Now that we get a positive integer m (1≤x≤m≤30), your job is to look for M to I with J. This m meets the following conditions for I and J:
Sum (i1,j1) +sum (i2,j2) +sum (i3,j3) +...+sum (im,jm) is the largest in this sequence.
Note: the intersection of any two intervals [ix,jx] and [iy,jy] is an empty set.

Please find out: what is the maximum sum (i1,j1) +sum (i2,j2) +sum (i3,j3) +...+sum (im,jm)?

Input Format

The first row of two integers n,m.
The second line is an n integer separated by a space, that is, A1~an.

output Format

Please output the maximum value of our defined: sum (i1,j1) +sum (i2,j2) +sum (i3,j3) +...+sum (im,jm), which is the largest m sub-segment and.

Sample Data 1input
3 1
1 2 3
Output
6
Sample Data 2input
6 2
-1 4-2 3-2 3
Output
8
Problem Analysis

The problem of "f[j][i x, divided into y-parts", usually DP is a "," which means dividing the first J elements into the most * values of the I Parts.

The problem is the same: f[j][i] as mentioned above, there are two decisions for newly added elements:

    1. Add to the last paragraph of the previous One.
    2. Separate into Segments.

The transfer equation is: $f [j][i] = max (f[j-1][i] + val[i], max_{k = 1}^{j-1}\{f[j-1][k]\} + val[i]) $

A little calculation, you will find that such a space has been report. The next step is the scrolling array.

It can be found that the J dimension of the transfer equation is always shifted from the last j-1, and I is not changed, and f[j-1][k] is only required to be the maximum value of the last time, then the following optimizations can be made using a scrolling array:

1. Remove the I this dimension, and I this one dimension refers to the outer cycle, the guarantee j-1 to J is I this one dimension. Array requires only f[n]

2. Use mx[j-1] to represent $max_{k = 1}^{j-1}\{f[j-1][k]\} $, with each internal loop (j) update for the next outer loop (i).

So the shift becomes:

 for (int1; I <= m; i++) {        =-oo;          for (int j = i; J <= n; J + +) {            11] + val[j]            ); 1 ]);             1] = tmp;        }    }
Code
#include <iostream>#include<cstdio>#include<cstdlib>#include<algorithm>#include<cstring>#include<string>#include<cmath>using namespacestd;Const intN = 1e6 +5, oo =0x7fffffff;intn, m;typedefLong Longll;ll f[n], mx[n], val[n];inline ll read () {ll i=0, F =1;CharCH =GetChar ();  for(; (ch <'0'|| CH >'9') && ch! ='-'; CH =GetChar ()); if(ch = ='-') F =-1, ch =GetChar ();  for(; CH >='0'&& CH <='9'; CH =GetChar ()) i= (i <<3) + (i <<1) + (ch-'0'); returnIf;} InlinevoidWR (ll X) {if(x <0) Putchar ('-'), x =-x; if(x >9) WR (x/Ten); Putchar (x%Ten+'0');}intmain () {n= Read (), m =Read ();  for(inti =1; I <= n; I++) val[i] =Read ();  for(inti =1; I <= m; i++) {ll tmp= -oo;  for(intj = i; J <= n; J + +) {f[j]= Max (f[j-1] + val[j], mx[j-1] +val[j]); TMP= Max (tmp, f[j-1]); Mx[j-1] =tmp; }} ll ans= -oo;  for(inti = m; I <= n; i++) ans=Max (ans, f[i]); WR (ans), Putchar ('\ n'); return 0;}

"max m and" dp + scrolling arrays

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.