HDU 1024 Max Sum Plus Plus
来源:互联网 发布:工商信息查询网大数据 编辑:程序博客网 时间:2024/04/30 16:41
Max Sum Plus Plus
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 16803 Accepted Submission(s): 5525
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 ≤ jxor 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 ≤ jxor 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
68HintHuge input, scanf and dynamic programming is recommended.
Author
JGShining(极光炫影)
解题思路:求n个数的m个不相交子区间的和的最大值,令dp[i][j]表示以第i个数为结尾的j个区间的最大值,
dp[i][j]=max(dp[i-1][j]+arr[i],max(dp[k][j-1])+arr[i],0<k<i);
注意到dp[i][j]只与dp[i-1][j]和dp[k][j-1]有关,采用滚动数组优化
同时dp[k][j-1]可以在之前的0---i-1中计算出来用数组保存下
#include <iostream>#include <cstdio>#include <cstring>#include <string>#include <algorithm>#include <set>#include <map>#include <list>#include <queue>#include <stack>#include <deque>#include <vector>#include <bitset>#include <cmath>#include <utility>#define Maxn 1000005#define Maxm 1000005#define lowbit(x) x&(-x)#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1#define PI acos(-1.0)#define make_pair MP#define LL long long #define Inf (1LL<<62)#define inf 0x3f3f3f3f#define re freopen("in.txt","r",stdin)#define wr freopen("out.txt","w",stdout)using namespace std;int arr[Maxn];int dp[Maxn];int dpp[Maxn];int read(){ int res=0; bool flag=true; while(1) { char ch=getchar(); if(ch>='0'&&ch<='9') res=res*10+ch-'0'; else if(ch=='-') { flag=false; continue; } else break; } return flag?res:-res;}int main(){ int n,m; int ans; //re;wr; while(~scanf("%d%d",&m,&n)) { getchar(); for(int i=0;i<=n;i++) arr[i]=dp[i]=dpp[i]=0; for(int i=1;i<=n;i++) arr[i]=read(); for(int j=1;j<=m;j++) { ans=-inf; for(int i=j;i<=n;i++) //下标要从j开始,这样才能保证滚动数组是正确的 { dp[i]=max(dpp[i-1],dp[i-1])+arr[i];//dp[i][j]=max(dp[i-1][j]+arr[i],max(dp[k][j-1])+arr[i],0<k<i) dpp[i-1]=ans;//dpp存储max(dp[k][j-1]),0<k<i ans=max(ans,dp[i]);//ans表示以i为结尾的选择了j段的最大值 } } printf("%d\n",ans); } return 0;}
0 0
- hdu 1024Max Sum Plus Plus
- hdu 1024 Max Sum Plus Plus--DP
- hdu 1024 Max Sum Plus Plus
- hdu 1024 Max Sum Plus Plus
- hdu 1024 Max Sum Plus Plus
- hdu 1024 Max Sum Plus Plus
- hdu 1024 Max Sum Plus Plus
- hdu 1024 Max Sum Plus Plus(dp)
- hdu 1024 max sum plus plus
- HDU 1024 Max Sum Plus Plus
- hdu 1024 Max Sum Plus Plus
- HDU 1024 Max Sum Plus Plus
- 【HDU 1024】 Max Sum Plus Plus
- hdu 1024 Max Sum Plus Plus
- HDU 1024 Max Sum Plus Plus
- hdu-1024-Max Sum Plus Plus-DP
- hdu 1024 Max Sum Plus Plus
- HDU 1024Max Sum Plus Plus
- 行为设计模式---观察者模式(Observer)
- Word Break
- Hibernate,一对一的关联关系
- CalTech machine learning video 5 note , training vs. testing
- 2.1.7保留-表格相关元素
- HDU 1024 Max Sum Plus Plus
- gps纠偏
- 做技术的时间长了,越来越觉得分享也是一种快乐
- Eucalyptus常用查询命令
- CalTech machine learning, video 6 note,theory of generalization
- OS—操作系统
- Android :Activity中finish() onDestroy() 和System.exit()的区别
- 开源的搜索引擎
- SystemParametersInfo GetSystemMetrics