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
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. ^_^
Process to the end of file.
#include<bits/stdc++.h>
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<dp[j]) maxn=dp[j];
//pre[j]=maxn;
}
//for(int l=0;l<=n;l++) cout<<pre[l]<<" ";
//cout<<endl;
}
cout<<maxn<<endl;
}
return 0;
}