hdu 1024 最大M子段dp

题目:

Max Sum Plus Plus

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


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
Hint
Huge input, scanf and dynamic programming is recommended.
与求最大连续子段略有不同,此题目要求求出m个连续子段的sum,且使得sum尽量大;
考虑状态转移方程:f{i,j}表示前j个元素分为i段的最大值,则有   f{i,j}=max{f{i,j-1},max{i-1,k}}+a[j],(i-1<=k
两种决策:
-1-:  前j个元素分为i段切最后一段以a结尾;
-2-:aj独自占一段,剩下的i-1段选前k(i-1<=k
n为100w考虑使用滚动数组,pre[j]保存上一个i对应的j的的最大值,dp[j]为当前正在计算的i的j的最大值;
代码:

#include
using namespace std;
const int inf=999999999;
int dp[1000005],a[1000005],pre[1000005];
int main()
{
int n,m,i,j,k,t;
while (cin>>m>>n){int maxn=-inf;t=0;
memset(dp,0,sizeof(dp));
memset(pre,0,sizeof(pre));
for (i=1;i<=n;i++) scanf("%d",&a[i]);

for (i=1;i<=m;i++){
maxn=-inf;                                              //每一轮开始都将maxn初始化
for (j=i;j<=n;j++){
dp[j]=max(pre[j-1],dp[j-1])+a[j];             //先使用在更新上一轮的值
pre[j-1]=maxn;                                         //此处很重要,不可写成下文注释的形式,因为一旦那样写下一个j计算时用到的j-1变成了本轮的最大值的意思,概念就变了

if (maxn//pre[j]=maxn;
}
//for(int l=0;l<=n;l++) cout<//cout< }
cout< }
return 0;
}

转载于:https://www.cnblogs.com/zzqc/p/6412848.html

你可能感兴趣的:(hdu 1024 最大M子段dp)