Max Sum Plus Plus

A - Max Sum Plus Plus

Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

Submit Status

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

Hint

 Huge input, scanf and dynamic programming is recommended.
         

/*
题意:给你一个长度为n的序列,让你求出最大m个字段的序列元素和

初步思路:动态规划最大m字段和,dp数组,dp[i][j]表示以a[j]结尾的,i个字段的最大和

    两种情况:1.第a[j]元素单独作为第i个字段
            2.第a[j]元素和前面的字段共同当做第i个字段

    得到状态转移方程:dp[i][j]=max( dp[i][j-1]+a[j] , max(dp[i-1][t])+a[j]);

    但是实际情况是,时间复杂度和空间复杂度都是相当的高,所以要进行时间和空间的优化:
        将每次遍历的时候的max(dp[i-1][t]) 用一个数组d储存起来,这样就能省去寻找max(dp[i-1][t])的时间,
        这样状态转移方程就变成了 dp[i][j]=max( dp[i][j-1]+a[j] , d[j-1]+a[j]), 会发现dp数组的可以
        省去一维,因为每次都是和前一次的状态有关,所以可以记录前一次状态,再用一个变量tmp记录下dp[i][j-1],
        这样方程就变成了 dp[i][j]=max( tmp+a[j] , d[j-1]+a[j]);这样就可以化简一下就是:dp[i][j]=
        max( tmp , d[j-1])+a[j];

*/
#include <bits/stdc++.h>
#define N 1000005
using namespace std;
int a[N];
int n,m;
int d[N];//用来存储j-1的位置用来存储 max(dp[i-1][t])
int main(){
    // freopen("in.txt","r",stdin);
    while(scanf("%d%d",&m,&n)!=EOF){
        memset(d,0,sizeof d);
        for(int i=1;i<=n;i++){
            scanf("%d",&a[i]);
        }
        /*
            dp[i][j]=max( dp[i][j-1]+a[j] , max(dp[i-1][t])+a[j])
        */
        for(int i=1;i<=m;i++){//遍历字段
            int tmp = 0;//用来记录dp[i-1][j]
            for(int k = 1; k <= i; ++k)
                tmp += a[k];
            //由于d[n]的位置是永远都用不到的,所以就用来存储最后的姐
            d[n] = tmp;//前面的i项,每项都是一个段的时候

            for(int j = i+1; j <= n; ++j)
            {
                tmp = max(d[j-1], tmp) + a[j]; //a[j]单独作为一个段的情况 和 前面的max(dp[i-1][t])

                d[j-1] = d[n];//将这个值保存下来

                d[n] = max(d[n], tmp); //比较大小方便答案的输出
            }
        }
        printf("%d\n",d[n]);
    }
    return 0;
}
时间: 2024-10-01 02:41:40

Max Sum Plus Plus的相关文章

HDU 1003 Max Sum

题目: Problem Description Given a sequence a[1],a[2],a[3]......a[n], your job is to calculate the max sum of a sub-sequence. For example, given (6,-1,5,4,-7), the max sum in this sequence is 6 + (-1) + 5 + 4 = 14. Input The first line of the input cont

HDU 1003 Max Sum【动态规划求最大子序列和详解 】

Max Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 250714    Accepted Submission(s): 59365 Problem Description Given a sequence a[1],a[2],a[3]......a[n], your job is to calculate the max su

HDU 1024 Max Sum Plus Plus --- dp+滚动数组

HDU 1024 题目大意:给定m和n以及n个数,求n个数的m个连续子系列的最大值,要求子序列不想交. 解题思路:<1>动态规划,定义状态dp[i][j]表示序列前j个数的i段子序列的值,其中第i个子序列包括a[j], 则max(dp[m][k]),m<=k<=n 即为所求的结果 <2>初始状态: dp[i][0] = 0, dp[0][j] = 0; <3>状态转移: 决策:a[j]自己成为一个子段,还是接在前面一个子段的后面 方程: a[j]直接接在前面

[2016-03-28][HDU][1024][Max Sum Plus Plus]

时间:2016-03-28 17:45:33 星期一 题目编号:[2016-03-28][HDU][1024][Max Sum Plus Plus] 题目大意:从n个数字提取出一定数字组成m个部分,使得这个部分的总和最大 分析: dp[i][j]表示前i段计算第j个数字,dp[i][j] = max(dp[i - 1][j - 1] + a[j],dp[i][k] + a[j]); #include <algorithm> #include <cstring> #include &

HDU 1024 Max Sum Plus Plus

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1024 Max Sum Plus Plus Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 21926    Accepted Submission(s): 7342 Problem Description Now I think you ha

Hdoj 1024 Max Sum Plus Plus 【DP】

Max Sum Plus Plus Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 18861 Accepted Submission(s): 6205 Problem Description Now I think you have got an AC in Ignatius.L's "Max Sum" problem. To b

hdoj 1003 Max Sum 【最大子段和】【贪心】

题意:... 策略:看着像贪心,感觉也是贪心. 很久之前做的,又做了一遍,好题. 代码: #include<stdio.h> #include<string.h> int s[100005]; int main() { int t, i, j, l, st, en, n, v = 1; scanf("%d", &t); while(t --){ scanf("%d", &n); for(i = 1; i <= n; i

杭电 1003 Max Sum

http://acm.hdu.edu.cn/showproblem.php?pid=1003 Max Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 142781    Accepted Submission(s): 33242 Problem Description Given a sequence a[1],a[2],a[3

HDU 3415 Max Sum of Max-K-sub-sequence 单调队列题解

本题又是一题单调队列题解. 技巧就是需要计算好前n项和Sn = a1 + a2 + ... an 这样方便处理. 记录一条单调队列,其意义是: q(head), q(head+1), ...q(tail) 其中头q(head)代表当前最佳解的起点 这样我们只需要在求某点为结尾的S[i] - S[q(head)就得到当前最佳值. 了解了单调数列,知道其中的记录意义,那么这道题就没有难度了.我也是了解这些信息之后就自己敲出代码的. 不过有些细节没写好也让我WA了几次. 最近少刷水题,而一直都是每天一

HDU 1024 Max Sum Plus Plus【动态规划求最大M子段和详解 】

Max Sum Plus Plus Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 29942    Accepted Submission(s): 10516 Problem Description Now I think you have got an AC in Ignatius.L's "Max Sum" problem