Max Sum Plus Plus

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

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][0] ... 表示前i个数分成j个组,不选第i个数的最大得分

dp[i][j][1] ... 表示前i个数分成j个组,选第i个数的最大得分

因为状态i只跟状态i-1, 所以可以用滚动数组来减空间

取最要自己写 。 否则卡常数会超时

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm> using namespace std ;
const int N = ;
const int inf = 1e9+; int dp[][N][] , n , m , x[N] ;
inline int MAX( int a , int b ) {
if( a > b ) return a ;
else return b ;
}
int main() {
// freopen("in.txt","r",stdin);
while( ~scanf("%d%d",&m,&n) ) {
for( int i = ; i <= n ; ++i ) {
scanf("%d",&x[i]);
}
int v = ;
dp[v][][] = ;
dp[v][][] = x[] ;
for( int i = ; i < n ; ++i ) {
for( int j = ; j <= i + && j <= m ; j++ ) {
dp[v^][j][] = dp[v^][j][] = -inf ;
}
for( int j = min( m , i ) ; j >= ; --j ) {
if( j != i ) {
dp[v^][j+][] = MAX( dp[v][j][] + x[i+] , dp[v^][j+][] );
dp[v^][j][] = MAX( dp[v][j][] , dp[v^][j][]);
}
if( j != ) {
dp[v^][j][] = MAX ( dp[v^][j][] , dp[v][j][] + x[i+] ) ;
dp[v^][j+][] = MAX ( dp[v^][j+][] , dp[v][j][] + x[i+] ) ;
dp[v^][j][] = MAX ( dp[v^][j][] , dp[v][j][] ) ;
}
}
v ^= ;
}
int ans = -inf ;
if( m < n ) ans = MAX( ans , dp[v][m][] );
if( m > ) ans = MAX( ans , dp[v][m][] );
printf("%d\n",ans);
}
return ;
}

HDU 1024 Max Sum Plus Plus (递推)的更多相关文章

  1. HDU 1024 Max Sum Plus Plus --- dp+滚动数组

    HDU 1024 题目大意:给定m和n以及n个数,求n个数的m个连续子系列的最大值,要求子序列不想交. 解题思路:<1>动态规划,定义状态dp[i][j]表示序列前j个数的i段子序列的值, ...

  2. HDU 1024 Max Sum Plus Plus (动态规划)

    HDU 1024 Max Sum Plus Plus (动态规划) Description Now I think you have got an AC in Ignatius.L's "M ...

  3. HDU 1024 Max Sum Plus Plus(m个子段的最大子段和)

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1024 Max Sum Plus Plus Time Limit: 2000/1000 MS (Java/ ...

  4. HDU 1024 max sum plus

    A - Max Sum Plus Plus Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I6 ...

  5. HDU 1024 Max Sum Plus Plus【动态规划求最大M子段和详解 】

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

  6. hdu 1024 Max Sum Plus Plus DP

    Max Sum Plus Plus Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php ...

  7. hdu 1024 Max Sum Plus Plus

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

  8. HDU 1024 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 ...

  9. HDU 1024 Max Sum Plus Plus(DP的简单优化)

    Problem Description Now I think you have got an AC in Ignatius.L's "Max Sum" problem. To b ...

随机推荐

  1. java:集合输出之foreach输出三

    java:集合输出之foreach输出三 foreach输出: List<String> allList = new ArrayList<String>(); allList. ...

  2. Java面试之基础篇(4)

    31.String s = new String("xyz");创建了几个StringObject?是否可以继承String类? 两个或一个都有可能,”xyz”对应一个对象,这个对 ...

  3. extjs计算两个DateField所间隔的月份(天数)

    需求:两个DateField控件,分别为开始时间和结束时间.当选择完结束时间后,自动计算这两个时间段所间隔的月或天数. 需要解决的问题: 1.直接使用Ext.getCmp('endDate').get ...

  4. 如何生成各种mif文件,绝对经典!!!

    mif文件生成模板,只需要5步,很简单!!!!! 先说明如何操作,1-2-3-4-5步,后面附上模板!!! 下面以汉字去模演示过程: 1.取模软件设置:注意这里是设置的输出数据的格式!!!!!!!!! ...

  5. python图像处理——频率域增强

    图像的傅里叶变换: import chardet import numpy as np import cv2 as cv import cv2 from PIL import Image import ...

  6. Scrapy 学习笔记爬豆瓣 250

    Scrapy 是比较上层的库,基于中间层开发,它基于高层,所以它依赖许多其它库.事件驱动的异步技术. Scrapy 爬取网页,以豆瓣电影 Top 250 为例子. 首先打开命令提示符,输入.scrap ...

  7. Java数据结构与算法(4):二叉查找树

    一.二叉查找树定义 二叉树每个节点都不能有多于两个的儿子.二叉查找树是特殊的二叉树,对于树中的每个节点X,它的左子树中的所有项的值小于X中的项,而它的右子树中所有项的值大于X中的项. 二叉查找树节点的 ...

  8. Retrotranslator使用简介(JDK1.5->1.4)

      Retrotranslator是一个可以把JDK1.5(6)下编译的类(或包)转译成JDK1.4下可以识别的类(包)的工具. 为现在还用JDK1.4呢?我想无非是现在的大部分Java Web应用是 ...

  9. JavaScript 查看stack trace

    How can I get a JavaScript stack trace when I throw an exception? Edit 2 (2017): In all modern brows ...

  10. 整体二分初探 两类区间第K大问题 poj2104 & hdu5412

    看到好多讲解都把整体二分和$CDQ$分治放到一起讲 不过自己目前还没学会$CDQ$分治 就单独谈谈整体二分好了 先推荐一下$XHR$的 <浅谈数据结构题的几个非经典解法> 整体二分在当中有 ...