首页 > 代码库 > hdu-1024 Max Sum Plus Plus
hdu-1024 Max Sum Plus Plus
Max Sum Plus Plus
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 30478 Accepted Submission(s): 10750
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. ^_^
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.
Process to the end of file.
Output
Output the maximal summation described above in one line.
Sample Input
1 3 1 2 32 6 -1 4 -2 3 -2 3
Sample Output
6 8
这题的题意:
这题的题意:
设输入的数组为a[1...n],从中找出m个段,使者几个段的和为最大;
dp[i][j]表示前j个数中取i个段的和的最大值,其中最后一个段包含a[j]。
dp[i][j]=max(dp[i][j-1],dp[i-1][k])+a[j],
前一个表示j与j-1在i组里,后一个表示j单独成组。
这个代码会爆内存。
#include<iostream>#include<algorithm>#include<stdio.h>#include<string.h>using namespace std;int dp[1000][1000];int main(){ int n,m; int a[100000]; while(cin>>n>>m) { for(int i=0;i<m;i++) cin>>a[i]; memset(dp,0,sizeof dp); for(int i=1;i<=n;i++) { for(int j=0;j<m;j++) { int t=0; for(int k=0;k<j;k++) { t=max(t,dp[i-1][k]); } dp[i][j]=a[j]+max(dp[i][j-1],t); } } int mx=0; for(int i=0;i<m;i++) { mx=max(mx,dp[n][i]); } cout<<mx<<endl; } return 0;}
n 1,000,000,m不知道,开一个2维dp数组会炸内存。
那么我们用 一个滚动数组去更新
因为
dp[i][j]=max(dp[i][j-1],dp[i-1][k])+a[j],
第i个区间之和i-1一个有关
那么dp[t][j]=max(dp[t][j-1],dp[1-t][k])+a[j];
#include<string.h>using namespace std;int dp[2][100000];int main(){ int n,m; int a[100000]; while(cin>>n>>m) { for(int i=0;i<m;i++) cin>>a[i]; memset(dp,0,sizeof dp); int t; for(int i=1;i<=n;i++) { t=i%2; for(int j=0;j<m;j++) { int q=0; for(int k=0;k<j;k++) { q=max(q,dp[1-t][k]); } dp[t][j]=a[j]+max(dp[t][j-1],q); } } int mx=0; for(int i=0;i<m;i++) { mx=max(mx,dp[t][i]); } cout<<mx<<endl; } return 0;}
你以为换完滚动数组就好了吗。不纯在的。超时
现在这个算法是n的3次方
那我们就要想用快一点的
考虑我们不需要j-1之前的最大和具体发生在k位置,只需要在j-1处记录最大和即可用pre[j-1]记录即可
pre[j-1],不包括a[j-1]的j-1之前的最大和
则dp[t][j]=max(dp[t][j-1],pre[j-1])+a[j]
此时可以看见,t这一维也可以去掉了
即最终的状态转移为
dp[j]=max(dp[j-1],pre[j-1])+a[j];
#include<iostream>#include<algorithm>#include<stdio.h>#include<string.h>using namespace std;int dp[100005];int pre[100005];int main(){ int n,m; int a[100005]; while(~scanf("%d%d",&n,&m)) { for(int i=1;i<=m;i++) scanf("%d",&a[i]); memset(dp,0,sizeof dp); memset(pre,0,sizeof pre); int mx; for(int i=1;i<=n;i++) { mx=-999999999; for(int j=i;j<=m;j++) { dp[j]=max(dp[j-1],pre[j-1])+a[j]; pre[j-1]=mx; mx=max(mx,dp[j]); } } printf("%d\n",mx); } return 0;}
hdu-1024 Max Sum Plus Plus
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。