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 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.
Author
JGShining(极光炫影)
题意:求在给出的n个数中找出m个连续的子序列,使得各子序列之和最大。
解题链接
代码(不是太懂,什么时候懂了再来补一个详细解释吧):
#include <cstdio>
#include <iostream>
#include <cstring>
const int M = 1e6+5;
#define LL __int64
using namespace std;
LL pre[M], num[M];
inline LL max(LL A, LL B){
return A > B? A:B;
}
void DP(int m, int n){
LL temp; //temp就是DP[I][J]
for(int i = 1; i <= m; ++ i){
temp = 0;
for(int j = 1; j <= i; ++ j) temp += num[j];
pre[n] = temp;
for(int j = i+1; j <= n; ++ j){
temp = max(temp, pre[j-1])+num[j]; //pre[j-1]就是max(dp[i-1][t])(i-1 <= t <= i-1);
pre[j-1] = pre[n];
pre[n] = max(temp, pre[n]);
}
}
}
int main(){
int n, m;
while(scanf("%d%d", &m, &n) == 2){
for(int i = 1; i <= n; ++ i){
scanf("%I64d", num+i); pre[i] = 0;
}
DP(m, n);
printf("%I64d\n", pre[n]);
}
return 0;
}
Hdoj 1024 Max Sum Plus Plus 【DP】
原文:http://blog.csdn.net/shengweisong/article/details/44966977