题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=1024
Max Sum Plus Plus
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 31798 Accepted Submission(s): 11278
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.
2 6 -1 4 -2 3 -2 3
8
Huge input, scanf and dynamic programming is recommended.
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
#define ms(a,b) memset((a),(b),sizeof((a)))
using namespace std;
typedef long long LL;
const double EPS = 1e-;
const int INF = 2e9;
const LL LNF = 2e18;
const int MAXN = 1e6+; int a[MAXN];
int last_max[MAXN], all_max[][MAXN];
int main()
{
int n, m;
while(scanf("%d%d",&m, &n)!=EOF)
{
for(int i = ; i<=n; i++)
scanf("%d", &a[i]); ms(all_max, );
ms(last_max, ); for(int i = ; i<=m; i++)
{
last_max[i] = all_max[i&][i] = last_max[i-]+a[i];
for(int j = i+; j<=n; j++)
{
last_max[j] = max(last_max[j-], all_max[!(i&)][j-]) + a[j];
all_max[i&][j] = max(all_max[i&][j-], last_max[j]);
/**以上代码的实际意义为:
last_max[i][j] = max(last_max[i][j-1], all_max[i-1][j-1]) + a[j];
all_max[i][j] = max(all_max[i][j-1], last_max[i][j]);
**/
}
}
printf("%d\n", all_max[m&][n]);
}
return ;
}