当前位置:网站首页>HDU 1024 (rolling array + dynamic programming)

HDU 1024 (rolling array + dynamic programming)

2021-07-28 11:35:49 wx60f7c9afba268

Max Sum Plus Plus

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 23697    Accepted Submission(s): 8094


Problem Description
Now I think you have got an AC in Ignatius.L's "Max Sum" problem. To be a brave ACMer, we always challenge ourselves to more difficult problems. Now you are 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 an integer m (m > 0), your task is to find m pairs of i and j which make sum(i 1, j 1) + sum(i 2, j 2) + sum(i 3, j 3) + ... + sum(i m, j m) maximal (i x ≤ i y ≤ j x or i x ≤ j y ≤ j x is not allowed).

But I`m lazy, I don't want to write a special-judge module, so you don't have to output m pairs of i and j, just output the maximal summation of sum(i x, j x)(1 ≤ x ≤ m) instead. ^_^
 

 

Input
Each test case will begin with two integers m and n, followed by n integers S 1, S 2, S 3 ... S n.
Process to the end of file.
 

 

Output
Output the maximal summation described above in one line.
 

 

Sample Input
1 3 1 2 3 2 6 -1 4 -2 3 -2 3
 

 

Sample Output
6 8
 
Didn't do it .. Can only think of two-dimensional , The scrolling array is really powerful .

The question :n A number divided into m The maximum sum of segments
analysis :dp[i][j] Representative to a[j] Before the end j A number is divided into i The maximum sum of segments
You can get : 1. If a[j] Separate into sections dp[i][j] = dp[i-1][k] + a[j] among 1<=k<j It means before 1- k A number makes up i-1 paragraph .
             2. If a[j] Incorporated into i paragraph that dp[i][j]=dp[i][j-1]+a[j] Analysis leads to dp[i][j] = max(1,2)
But the condition of this question is that it is not allowed to do so , Enumerate first i , j ,k The time complexity of is O(n^3) dp The space of the array should be O(n^2), And the amount of data has reached 1000000 Obviously... Is not allowed . therefore , Here we need to use a new idea : Scrolling array .
We can see dp[i][j] Only and whether it contains a[j] relevant , So here we can group the current state and the previous state with two one dimensions .
New status : dp[j] Said to a[j] Before the end i The maximum sum of segments ,pre[j] Before presentation j An array before i The maximum sum of segments , Not necessarily including a[j]
dp[j] = max(dp[j-1]+a[j],pre[j-1]+a[j])

/** The question :n A number divided into m The maximum sum of segments */
/// analysis :dp[i][j]  Representative to a[j] Before the end j A number is divided into  i  The maximum sum of segments 
/// You can get : 1. If a[j] Separate into sections  dp[i][j] = dp[i-1][k] + a[j]  among  1<=k<j  It means before 1- k  A number makes up i-1 paragraph .
///          2. If a[j] Incorporated into i paragraph   that dp[i][j]=dp[i][j-1]+a[j]  Analysis leads to  dp[i][j] = max(1,2)
/// But the condition of this question is that it is not allowed to do so  , Enumerate first  i , j ,k  The time complexity of is  O(n^3) dp The space of the array should be  O(n^2), And the amount of data has 
/// Arrived at the  1000000  Obviously... Is not allowed . therefore , Here we need to use a new idea : Scrolling array .
/// We can see dp[i][j] Only and whether it contains a[j] relevant , So here we can group the current state and the previous state with two one dimensions .
/// New status : dp[j] Said to a[j] Before the end i The maximum sum of segments ,pre[j] Before presentation j An array before i The maximum sum of segments , Not necessarily including a[j]
///dp[j] = max(dp[j-1]+a[j],pre[j-1]+a[j])
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<math.h>
#include<algorithm>
using namespace std;
const int N = 1000005;
int a[N];
int dp[N];
int pre[N];
int main()
{
    int m,n;
    while(scanf("%d%d",&m,&n)!=EOF){
        for(int i=1;i<=n;i++){
            scanf("%d",&a[i]);
            dp[i]=pre[i]=0;
        }
        dp[0]=pre[0]=0;
        int mam;
        for(int i=1;i<=m;i++) {/// Enumerate each paragraph 
            mam = -0x7fffffff;
            for(int j=i;j<=n;j++){
                dp[j] = max(dp[j-1]+a[j],pre[j-1]+a[j]);
                pre[j-1] = mam;  /// Before presentation  j-1  It's made up of numbers i The maximum sum that a segment can represent 
                mam=max(dp[j],mam);
            }
        }
        printf("%d\n",mam);
    }
    return 0;
}

 

版权声明
本文为[wx60f7c9afba268]所创,转载请带上原文链接,感谢
https://chowdera.com/2021/07/20210721160049499W.html