Max Sum Plus Plus

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 35485    Accepted Submission(s): 12639

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.

状态dp[i][j]有前j个数,组成i组的和的最大值。决策: 
第j个数,是在第包含在第i组里面,还是自己独立成组。
方程 dp[i][j]=Max(dp[i][j-1]+a[j] , max( dp[i-1][k] ) + a[j] ) 0<k<j
空间复杂度,m未知,n<=1000000, 继续滚动数组。
时间复杂度 n^3. n<=1000000. 显然会超时,继续优化。
max( dp[i-1][k] ) 就是上一组 0....j-1 的最大值。
我们可以在每次计算dp[i][j]的时候记录下前j个的最大值
用数组保存下来 下次计算的时候可以用,这样时间复杂度为 n^2.
#include <cstdio>
#include <algorithm>
#include <iostream>
#include <cstring>
using namespace std;
const int maxn = 1e6+;
const int INF = 0x7fffffff;
int dp[maxn];
int a[maxn];
int mmax[maxn];
int main(){
int n,m;
int maxx;
while(scanf("%d%d",&m,&n) !=EOF){
for(int i=;i<=n;i++){
scanf("%d",&a[i]);
mmax[i]=;
dp[i]=;
}
dp[]=;
mmax[]=;
for(int i=;i<=m;i++){
maxx=-*INF;
for(int j=i;j<=n;j++){
dp[j]=max(dp[j-]+a[j],mmax[j-]+a[j]);
mmax[j-]=maxx;
maxx=max(maxx,dp[j]);
}
}
printf("%d\n", maxx);
}
return ;
}

最新文章

  1. iOS 企业包碰到的问题
  2. MatLab/HR
  3. 【转】ASP.NET&quot;正在中止线程&quot;错误原因
  4. 项目中如果管理前端文件CSS和JS
  5. 将数据库表导入到solr索引
  6. 使用Fabric进行crash收集统计
  7. 51nod1274 最长递增路径
  8. Java垃圾回收器
  9. win7中CIFS挂载和解挂
  10. Yii 设置 flash消息 创建一个渐隐形式的消息框
  11. luogu【P2753】[USACO4.3]字母游戏Letter Game
  12. Ubuntu 16.04 安装Mysql 5.7 踩坑小记
  13. 快速实现兼容的js复制方式。有点非正规,通过非正规的排版实现。
  14. Python文本编辑器推荐
  15. 借助Docker单机秒开数十万TCP连接
  16. codeforces794D dfs+图上hash
  17. [svc]unix和cpu发展历史
  18. PHP isset 和 array_key_exists 对比
  19. 为什么推荐InnoDB引擎使用自增主键?
  20. Gravitee.io docker-compose运行

热门文章

  1. 如何快速查看mysql数据文件存放路径?
  2. 统计重复IP并排序
  3. 关于SSM框架项目中jsp页面EL表达式使用的一些疑问(一)
  4. contest0 from codechef
  5. &lt;s:submit&gt; 指定的method方法不执行
  6. 001.我的第一个Java程序
  7. SDK location not found. Define location with sdk.dir in the local.properties file or with an ANDROID
  8. Android stadio 工具使用
  9. sqlsever存储过程学习笔记
  10. 有关ViewPager的使用及解决Android下ViewPager和PagerAdapter中调用notifyDataSetChanged失效的问题