HDU 1024(DP+滚动数组)
2017-07-20 19:07
411 查看
Max Sum Plus Plus
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 29920 Accepted Submission(s): 10505
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 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. ^_^
Input
Each test case will begin with two integers m and n, followed by n integers S1, S2, S3 … Sn.
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.
Author
JGShining(极光炫影)
Recommend
题意:求m个不相交子段的和的最大值
做法:
dp[i][j]=max(dp[i][j-1],dp[i-1][k])+s[j]
dp[i][j]表示以s[j]结尾的i个子段的最大和
结果是MLE
优化:滚动数组。当面状态只与i-1与i有关。我们可以开个2*n数组就够了
即:dp[1][j]=max(dp[1][j-1],dp[0][k])+s[j]
结果TLE
可以记录一下dp[0][k]的之前最大值来直接和dp[1][j-1]比较
即 dp[j]=max(dp[j-1],pre[j-1])+s[j]
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 29920 Accepted Submission(s): 10505
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 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. ^_^
Input
Each test case will begin with two integers m and n, followed by n integers S1, S2, S3 … Sn.
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.
Author
JGShining(极光炫影)
Recommend
题意:求m个不相交子段的和的最大值
做法:
dp[i][j]=max(dp[i][j-1],dp[i-1][k])+s[j]
dp[i][j]表示以s[j]结尾的i个子段的最大和
结果是MLE
优化:滚动数组。当面状态只与i-1与i有关。我们可以开个2*n数组就够了
即:dp[1][j]=max(dp[1][j-1],dp[0][k])+s[j]
结果TLE
可以记录一下dp[0][k]的之前最大值来直接和dp[1][j-1]比较
即 dp[j]=max(dp[j-1],pre[j-1])+s[j]
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #define maxn 1000010 #define inf 0x3f3f3f3f3f3f using namespace std; typedef long long ll; ll n,m; int s[maxn]; int dp[maxn]; int pre[maxn]; void init() { memset(dp,0,sizeof(dp)); memset(pre,0,sizeof(pre)); } int main() { while(scanf("%I64d%64d",&m,&n)!=EOF) { init(); for(int i=1;i<=n;i++) scanf("%d",&s[i]); int tmp=-inf; for(int i=1;i<=m;i++) { tmp=-inf; for(int j=i;j<=n;j++) { dp[j]=max(dp[j-1],pre[j-1])+s[j]; pre[j-1]=tmp; tmp=max(dp[j],tmp); } } printf("%d\n",tmp); } return 0; }
相关文章推荐
- 【HDU 1024】Max Sum Plus Plus(DP+滚动数组优化+最大m段字段之和)
- hdu 1024 Max Sum Plus Plus(滚动数组)(DP)
- hdu 1024 Max Sum Plus Plus 一串数字中,m段连续数字最大和 滚动数组+dp
- 【hdu 1024】Max Sum Plus Plus —— dp && 滚动数组
- HDU-1024-DP-(滚动数组优化与状态转移)
- HDU 1024 Max Sum Plus Plus(DP+滚动数组)
- HDU 1024 Max Sum Plus Plus --- dp+滚动数组
- HDU 1024 Max Sum Plus Plus(普通dp && 滚动数组优化)
- HDU ~ 1024 ~ Max Sum Plus Plus(DP+滚动数组)
- HDU 1024 Max Sum Plus Plus (DP·滚动数组)
- HDU 1024 Max Sum Plus Plus【DP+滚动数组】
- hdu 1024 dp滚动数组
- DP ( 滚动数组 )——Max Sum Plus Plus ( HDU 1024 )
- hdu 1024 循环数组+简单dp
- 杭电ACM OJ 1024 Max Sum Plus Plus 动态规划 二维dp+滚动数组dp优化
- hdu 1024(滚动数组的学习)
- HDU - 1024 Max Sum Plus Plus (滚动数组)
- HDU 1024 最大M子段和 滚动数组优化
- HDU 4427 Math Magic【dp+优化+滚动数组】【好题】
- HDU - 2294 Pendant (DP滚动数组降维+矩阵快速幂)