HDOJ 1024 Max Sum Plus Plus 最大M字段和
来源:互联网 发布:汉诺塔递归算法java 编辑:程序博客网 时间:2024/05/23 01:18
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.
思路:动态规划
假设 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;}
阅读全文
0 0
- HDOJ 1024 Max Sum Plus Plus 最大M字段和
- HDOJ-1024 Max Sum Plus Plus (最大M子段和问题)
- hdoj Max Sum Plus Plus 1024 (DP) m个连续数组最大和
- hdoj 1024 Max Sum Plus Plus(m个子段最大和)
- HDOJ-1024 Max Sum Plus Plus (最大M子段和问题)
- hdoj 1024 Max Sum Plus Plus(最大m子段和)=-=
- hdu 1024 Max Sum Plus Plus(最大m字段和)
- hdoj Max Sum Plus Plus Plus 1244 (DP)m个连续段的最大和
- hdu 1024 Max Sum Plus Plus(DP最大字段和)
- 每日三题-Day1-B(HDOJ 1024 Max Sum Plus Plus 最大m子段和)
- hdu Max Sum Plus Plus(最大m段子段和)
- hdu Max Sum Plus Plus(最大m段子段和)
- m 段 最大和 max sum plus plus
- hdu1024 最大M个子段和 Max Sum Plus Plus
- hdu1042 Max Sum Plus Plus【最大M子段和】
- 【HDU 1024】Max Sum Plus Plus(DP+滚动数组优化+最大m段字段之和)
- hdu 1024 Max Sum Plus Plus 最大m个子序列
- hdoj 1024 Max Sum Plus Plus 【动态规划经典题目】【m子段和】
- Leetcode 320. Generalized Abbreviation
- 2017-7-5 : 快下班了写点东西
- POJ 2897 Dramatic Multiplications 笔记
- Scrapy阅读源码分析<三>
- SpringMVC工作原理
- HDOJ 1024 Max Sum Plus Plus 最大M字段和
- PHP Apache shutdown unexpectedly启动错误解释及解决的方法
- Windows编程_Lesson007_内核对象之二
- python-优矿-基金20%赎回赚钱概率96.3%
- mysql语法的注意事项
- java 泛型详解-绝对是对泛型方法讲解最详细的,没有之一
- Windows编程_Lesson008_内存
- “AR录取通知书”火了,HiAR独家揭示App嵌入AR的奥秘
- Android 全屏 但是有状态栏(任务栏)