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. ^_^
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.
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 8HintHuge input, scanf and dynamic programming is recommended.
思路:动态规划
假设 dp[i][j] 表示前 i 个 元素中 j 段的最大和,且第j段包含 a[i]. 则递推公式为
其中, 表示 将 a[i] 加入到 a[i-1] 所在的字段,
则是 从 j-1 到 i-1 中分成 j-1 段的最大值, a[i] 单独作为第j段的情况。
最后的结果为 。
代码如下:
#include<iostream>
#include<stdio.h>
#include<string>
#include<algorithm>
#include<memory.h>
using namespace std;
const int maxn = 1000000+5;
int main() {
//freopen("input.txt","r",stdin);
int a[maxn];
int n,m;
while(cin>>m>>n) {
if(m > n) {
cout<<0<<endl;
continue;
}
for(int i =1 ; i <= n; i++) cin>>a[i];
int dp[maxn][maxn];
memset(dp,0,sizeof(dp));
for(int j = 1; j <= m; j++) {
dp[j][j] = dp[j-1][j-1] + a[j];
int maxv = dp[j-1][j-1];
for(int i = j + 1; i + m - j <= n; i++) {
maxv = max(maxv,dp[i-1][j-1]);
dp[i][j] = max(dp[i-1][j],maxv) + a[i];
}
}
int res = INT_MIN;
for(int i = m; i <= n; i++) {
if(dp[i][m] > res) res = dp[i][m];
}
cout<<res<<endl;
}
return 0;
}
上面这种方法会超空间。 我们观察到 求解 dp[i][j] 时, 只用到了 j - 1 段 和 j 段时的值, 所以可以压缩空间。 在计算
代码如下:
#include<iostream>
#include<stdio.h>
#include<string>
#include<algorithm>
#include<memory.h>
using namespace std;
const int maxn = 1000000+5;
int main() {
//freopen("input.txt","r",stdin);
int a[maxn];
int n,m;
while(cin>>m>>n) {
if(m > n) {
cout<<0<<endl;
continue;
}
for(int i =1 ; i <= n; i++) cin>>a[i];
int dp[maxn][2];
int p = 0;
memset(dp,0,sizeof(dp));
for(int j = 1; j <= m; j++) {
dp[j][p] = dp[j-1][1-p] + a[j];
int maxv = dp[j-1][1-p];
for(int i = j + 1; i + m - j <= n; i++) {
maxv = max(maxv, dp[i-1][1-p]);
dp[i][p] = max(dp[i-1][p], maxv) + a[i];
}
p = 1 - p;
}
p = 1- p;
int res = INT_MIN;
for(int i = m; i <= n; i++) {
if(dp[i][p] > res) res = dp[i][p];
}
cout<<res<<endl;
}
return 0;
}