HDU1024 最大m子段和

Max Sum Plus Plus

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

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 S1, S2, S3, S4 ... Sx, ... Sn (1 ≤ x ≤ n ≤ 1,000,000, -32768 ≤ Sx ≤ 32767). We define a function sum(i, j) = Si + ... + Sj (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(i1, j1) + sum(i2, j2) + sum(i3, j3) + ... + sum(im, jm) maximal (ix ≤ iy ≤ jx or ix ≤ jy ≤ jx 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(ix, jx)(1 ≤ x ≤ m) instead. ^_^

Input

Each test case will begin with two integers m and n, followed by n integers S1, S2, S3 ... Sn.
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.

题意:

求最大m子段和。

代码:

//最大m子段和递推公式:dp[i][j]=max(dp[i][j-1]+num[j],dp[i-1][t]+num[j]) (i<=t<=j-1)
//优化:因为每次都要求一个最大的dp[i-1][t](i<=t<=j-1),可以每次求出来dp[i][j]时保存
//这个值,到下一次时直接用就行了,这样每次就只用到了dp[i][j]和dp[i][j-1],可以省去一维。
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn=1000006;
const int inf=0x7fffffff;
int dp[maxn],fdp[maxn],num[maxn];
int main()
{
    int m,n,maxnum;
    while(scanf("%d%d",&m,&n)==2){
        for(int i=1;i<=n;i++) scanf("%d",&num[i]);
        memset(dp,0,sizeof(dp));
        memset(fdp,0,sizeof(fdp));
        for(int i=1;i<=m;i++){
            maxnum=-inf;
            for(int j=i;j<=n;j++){
                dp[j]=max(dp[j-1]+num[j],fdp[j-1]+num[j]);
                fdp[j-1]=maxnum;
                maxnum=max(maxnum,dp[j]);
            }
        }
        printf("%d\n",maxnum);
    }
    return 0;
}
时间: 2024-11-15 01:50:06

HDU1024 最大m子段和的相关文章

hdu1024(m段子段和最大)

链接:点击打开链接 题意:n个数分成m段不相交子段和最大 代码: #include <stdio.h> #include <stdlib.h> #include <string.h> #include <iostream> #include <algorithm> using namespace std; const int INF=0x3f3f3f3f; int a[1000005],sum[1000005]; int dp[2][100000

HDU-1024题解(状态转移+最大连续子段和)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1024 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 mor

m大子段和 hdu1024

给出n个数,m个区间: 求选区m个区间的最大值: 1 #include<cstdio> 2 #include<algorithm> 3 #include<math.h> 4 #include<queue> 5 using namespace std; 6 int d[maxn]; 7 int pre[maxn]; 8 int main() 9 { 10 int m,n,tmp; 11 while(cin>>m>>n){ 12 int

hdu1024 Max Sum Plus Plus (Dp)

hdu1024 Max Sum Plus Plus 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 numbe

最大m子段和

参考自:最大m子段和总结与例题 51nod1052 HDU1024 题目介绍: 给定由n个整数(可能为负)组成的序列a1.a2.a3...,an, 以及一个正整数m,要求确定序列的m个不相交子段,使这m个子段的总和最大! 特别注意: 有些题目可能不存在负数答案,给出的序列全是负数,那么不管m是多少,答案是0.此时选择的子段是0个,不足m个,但符合题意... 也可能有些题目要求,必须选够m个子段. 区别在dp数组的初始化.前者要求dp初始为0,后者要求第0行为0,其余为负无穷 解题思路: 动态规划

最大子段和及其拓展

1.最大子段和问题 问题定义:对于给定序列a1,a2,a3--an,寻找它的某个连续子段,使得其和最大(如果某子序列全是负数则定义该子段和为 0).如( -2,11,-4,13,-5,-2 )最大子段是{ 11,-4,13 }其和为20. ·状态设计: dp[i] (1 <= i <= N) 表示以 a[i] 结尾的最大连续子段和. 显然,dp[i] >= 0 (1 <= i <= N) 状态转移方程:dp[i] = max{dp[i-1] + a[i], 0} (2 <

最大子段和(分治法)

#include <iostream> #include <algorithm> #include <cstdio> #include <cstring> #include <map> #define N 1000005 using namespace std; int a[1005]; int calc(int s,int e,int &l,int &r) { int l1,l2,l3,r1,r2,r3; if(s==e) re

最大连续子段和的两种线性算法

问题描述:给一个数组a1,a2,...,an.求这个数组的最大连续子段和.(非空子段) 即,定义Sij=ai+...+aj,则题目要求的是 max{Sij}(1<=i<=j<=n) N^3枚举和优化之后的N^2枚举就不说了,还有NlogN的二分算法也不提,想了解的可以看我的另一篇博客:http://www.cnblogs.com/itlqs/p/5097504.html 这里主要详解两种O(n)的算法. 方法一:动态规划 dp[i]表示以第i位为结尾的最大子段和.那么转移方程就是:dp[

算法重拾之路——最大子段和

***************************************转载请注明出处:http://blog.csdn.net/lttree******************************************** 第二章:动态规划 >最大子段和< 算法描述: ?给定由n个整数(可能为负整数)组成的序列 a1,a2, ... , an ,求该序列形如  从ai 到 aj (i ≤ j)的子段和的最大值.当所有整数均为负整数时定义其最大值为0.根据这个定义,所求的最优值为: