Max Sum Plus Plus

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

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.

 
Author
JGShining(极光炫影)
 

  1. #include<cstdio>
  2. #include<cstring>
  3. #include<iostream>
  4. using namespace std;
  5. const int N=1e6+;
  6. int n,m,a[N];
  7. //int f[N][N];
  8. int f[N],maxn[N];
  9. int main(){
  10. while(~scanf("%d%d",&m,&n)){
  11. memset(f,,sizeof f);
  12. memset(maxn,,sizeof maxn);
  13. for(int i=;i<=n;i++) scanf("%d",a+i);
  14. /*for(int i=1;i<=n;i++){
  15. for(int j=1;j<=m;j++){
  16. f[i][j]=f[i-1][j]+a[i];
  17. for(int k=1;k<i;k++){
  18. f[i][j]=max(f[i][j],f[k][j-1]+a[i]);
  19. }
  20. }
  21. }
  22. printf("%d\n",f[n][m]);*/
  23. int nowans=;
  24. for(int j=;j<=m;j++){
  25. nowans=-1e9;
  26. for(int i=j;i<=n;i++){
  27. f[i]=max(f[i-],maxn[i-])+a[i];
  28. maxn[i-]=nowans;
  29. nowans=max(nowans,f[i]);
  30. }
  31. }
  32. printf("%d\n",nowans);
  33. }
  34. return ;
  35. }

hdu1024 Max Sum Plus Plus[降维优化好题(貌似以后可以不用单调队列了)]的更多相关文章

  1. HDU1024 Max Sum Plus Plus (优化线性dp)

    Now I think you have got an AC in Ignatius.L's "Max Sum" problem. To be a brave ACMer, we ...

  2. [Codeforces712D] Memory and Scores(DP+前缀和优化)(不用单调队列)

    [Codeforces712D] Memory and Scores(DP+前缀和优化)(不用单调队列) 题面 两个人玩游戏,共进行t轮,每人每轮从[-k,k]中选出一个数字,将其加到自己的总分中.已 ...

  3. hdu1024 Max Sum Plus Plus 滚动dp

    Max Sum Plus Plus Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

  4. HDU1024 Max Sum Plus Plus 【DP】

    Max Sum Plus Plus Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

  5. HDU1024 Max Sum Plus Plus —— DP + 滚动数组

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=1024 Max Sum Plus Plus Time Limit: 2000/1000 MS ...

  6. 洛谷P2900 [USACO08MAR]土地征用Land Acquisition(动态规划,斜率优化,决策单调性,线性规划,单调队列)

    洛谷题目传送门 用两种不一样的思路立体地理解斜率优化,你值得拥有. 题意分析 既然所有的土地都要买,那么我们可以考虑到,如果一块土地的宽和高(其实是蒟蒻把长方形立在了平面上)都比另一块要小,那么肯定是 ...

  7. HDU-1024 Max Sum Plus Plus 动态规划 滚动数组和转移优化

    题目链接:https://cn.vjudge.net/problem/HDU-1024 题意 给n, m和一个序列,找m个不重叠子串,使这几个子串内元素和的和最大. n<=1e6 例:1 3 1 ...

  8. hdu1024 Max Sum Plus Plus的另一种解法

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1024 http://www.51nod.com/onlineJudge/questionCode.htm ...

  9. hdu1024 Max Sum Plus Plus

    动态规划,给定长度为n(≤1e6)的整数数组和整数m,选取m个连续且两两无交集的子区间,求所有方案中使得区间和最大的最大值. dp[i][j]表示结束位置(最后一个区间最后一个元素的位置)为i且选取区 ...

随机推荐

  1. JVM监控工具用法指导

    JVM监控工具用法指导 2010-09-27 15:39 dolphin-ygj javaeye.com 字号:T | T 通过JVM监控工具的使用可以及时发现问题,剔除安全隐患,这里向大家描述一下常 ...

  2. lua——string之string.gsub

    translated from the lua document string.gsub用法: 函数原型:string.gsub( s, pattern, rep1[, n] ) 函数功能:返回一个和 ...

  3. scp基本使用方法

    scp基本使用方法: scp用于在两台电脑之间进行数据的copy,形式如下:  第一种, scp [-r] 文件/文件夹  user@host:dir ,需要输入密码.  第二种, scp [-r] ...

  4. 多个git账号之间的切换

    在这篇文章中: 介绍 处理 一些题外话 我有一个repo,想要同时push到不同的仓库该如何设置? 我有一个github的repo,clone没有问题,push的时候总是报错:error: The r ...

  5. libmysqlclient16 libmysqlclient-dev

    如果提示安装 libmysqlclient16 则用 libmysqlclient-dev 代替之

  6. 安装版的tomcat微信支付时报2字节的UTF-8序列的字节2无效

  7. 封装自己的yQuery

    function myAddEvent(obj, sEv, fn) { if (obj.attachEvent) { obj.attachEvent('on' + sEv, fn) } else { ...

  8. navicat导入csv

    1.navicat for mysql 导入csv时出错,主要是由于csv中包含汉字所致: 2.解决办法为再host新连接时,选择高级,然后在编码菜单里选择20936 (Simplified Chin ...

  9. 15款最好的 jQuery 网格布局插件

    如今,大多数网站设计要靠网格系统和布局,这能够提供给设计人员一个方便的途径来组织网页上的内容.网格的设计最常见于报纸和杂志的版面,由文字和图像构成的列组成. 这篇文章给大家分享精心挑选的15款最佳的 ...

  10. android boot.img unpack pack

    每次编译boot.img都要花比较长的时间,有时候只是更改其中的配置文件. 如果能够将boot.img解压,更改之后再打包的话,就能节省时间. boot.img tools是别人写好的工具,能很好的解 ...