Max Sum Plus Plus(m个子段的最大和)
来源:互联网 发布:yy网络女主播不雅视频 编辑:程序博客网 时间:2024/05/16 10:32
Max Sum Plus Plus点击打开链接
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 30636 Accepted Submission(s): 10811
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
68HintHuge input, scanf and dynamic programming is recommended.
#include <cstdio>#include <cmath>#include <algorithm>using namespace std;const int MAX=99999;int main(){int n,m,Max,m1,a[MAX],b[MAX];while(~scanf("%d %d",&m,&n)){for(int i=1;i<=n;i++)scanf("%d",&a[i]);int t,j=1,v=1;t=n/m;while(j<m){Max=m1=0;for(int i=v;i<=t;i++){m1+=a[i];if(m1<0)m1=0;Max=max(Max,m1);}b[j++]=Max;v=t+1;t+=n/m;}//比如m=3,n=8 n/m=2 虽然n%m!=0,但是1 2 3 4 5 6 7 8在上边的while循环中竟然被分为了四段 哭晕 //第一次交没想到有n/m有余数的情况!!! 第二次就没有想到上面 7 8子段被重复计算且所分段数不符合要求的问题//最后想了想 可以用j来控制所分段数,让前m-1段和第m段分开算 Max=m1=0;for(int i=t-n/m+1;i<=n;i++){m1+=a[i];if(m1<0)m1=0;Max=max(Max,m1);}b[j++]=Max;int sum=0;for(int i=1;i<=m;i++)sum+=b[i];printf("%d\n",sum);}return 0;}
第三次提交前,发现我们不能硬生生的把这一串数字分为题目要求的子段个数,也就是说比如在这样一组数据3 8 1 3 -2 4 3 1 -5 2 ,按照我的方法会把该序列分成1 3,-2 4,3 1 -5 2。这样结果是4+4+4=12,很明显不对,只好找其他方法。看了两个小时这个链接里的解释,点击打开链接,点击打开链接才懂了,顺便把推理过程也放在这,省的忘了。现在就可以学习正确方法了。
题意:任意给你n个整数,让你求这n个整数的m个不相交子段和的最大值。
这道题是利用dp的思想,先定义 两个数组num[ n ],dp[ m][ n ],dp[i][j]表示由前j个数 ,组成的i个子段(最后一个子段包括num[ j ])的和的最大值。另外num[ j ]是否是自己组成一个子段。状态转移方程:dp[ i ][ j ]=max(dp[ i ][ j-1 ],dp[ i-1 ][ t])+num[ j ],1<=t<=j-1。
#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int MAX=1e6+5;int per_max[MAX],num[MAX];int DP(int m,int n)//DP是一种利用数组做题的思想,并不一定非得定义一个二维DP数组 {for(int i = 1 ;i <= m ;i ++){int tmp=0;for(int k = 1 ;k <= i ;k ++)tmp += num[k];per_max[n]=tmp;for(int j = i + 1 ;j <= n ;j ++){tmp = max (per_max[j-1],tmp)+num[j];//关键所在 per_max[j-1]=per_max[n];//这里赋给per_max[j-1],而不是per_max[j],就会把per_max[n]闲置出来留着优化per_max[n]= max(tmp,per_max[n]);}}return per_max[n];} int main(){int m,n;while(~scanf ("%d %d" ,&m ,&n)){for ( int i = 1 ;i <=n ;i++){scanf("%d",&num[i]);per_max[i]=0;}printf("%d\n", DP(m,n));}return 0;}
还有些地方理解的不够好,后面懂了再补充。阅读全文
0 0
- hdu1024 最大M个子段和 Max Sum Plus Plus
- Max Sum Plus Plus(m个子段的最大和)
- hdoj 1024 Max Sum Plus Plus(m个子段最大和)
- hdoj Max Sum Plus Plus Plus 1244 (DP)m个连续段的最大和
- hdu Max Sum Plus Plus(最大m段子段和)
- hdu Max Sum Plus Plus(最大m段子段和)
- m 段 最大和 max sum plus plus
- hdu1042 Max Sum Plus Plus【最大M子段和】
- hdu 1024 Max Sum Plus Plus 最大m个子序列
- HDACM1024 max sum plus plus:m个不相交子段的最大和
- hdu 1024 Max Sum Plus Plus(最大M子段和)
- HDOJ-1024 Max Sum Plus Plus (最大M子段和问题)
- hdu 1024 Max Sum Plus Plus(最大m子段和)
- HDU 1024 Max Sum Plus Plus(最大M子段和)
- hdu 1024 Max Sum Plus Plus(m段最大子列和)
- hdu 1024 最大M子段和 Max Sum Plus Plus(dp)(中等难度)
- HDU 1024 Max Sum Plus Plus(最大M段和)
- HDOJ-1024 Max Sum Plus Plus (最大M子段和问题)
- 数塔 【hdu-2084】【动态规划】
- 2017.8.8 魔兽地图DotR 思考记录
- SQL事务
- x264学习(2)——x264命令行参数解释
- 列表简介
- Max Sum Plus Plus(m个子段的最大和)
- react-native init DemoApp --version 0.44.3 解决 CFBundleIdentifier", Does Not Exist
- 【简单学】图表变变变
- Java基础—— Java概述
- Rikka with Graph (2017多校 图论规律题)
- 目录
- Spring中ClassPathXmlApplicationContext的简述
- 2017 Multi-University Training Contest
- c# 封装动态链接库dll