题目:
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 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. ^_^
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;
}