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. LOJ#2330 榕树之心 树形dp

    瞎扯 这个题和\(\mathsf{ISIJ2019 Au}\)神仙学弟\(\mathsf{\color{red}c}\mathsf{hangruinian2020}\)争辩了半个多小时. 概括一下就是 ...

  2. 【leetcode】1110. Delete Nodes And Return Forest

    题目如下: Given the root of a binary tree, each node in the tree has a distinct value. After deleting al ...

  3. STM32内部时钟设置-寄存器版

    STM32寄存器版本——内部时钟设置 同时要记得把延时初始化函数设置好 //系统时钟初始化函数 //pll:选择的倍频数,从2开始,最大值为16 //pll:选择的倍频数,这里使用内部时钟,PLL为4 ...

  4. java上传大文件(局域网环境)

    文件上传是最古老的互联网操作之一,20多年来几乎没有怎么变化,还是操作麻烦.缺乏交互.用户体验差. 一.前端代码 英国程序员Remy Sharp总结了这些新的接口 ,本文在他的基础之上,讨论在前端采用 ...

  5. 颜色空间模型 与 Opencv中的HSV模型范围

    颜色空间总结 RGB.HSV.YUV 什么是颜色 Wiki是这样说的:颜色或色彩是通过眼.脑和我们的生活经验所产生的一种对光的视觉效应.嗯,简单点说,颜色就是人对光的一种感觉,由大脑产生的一种感觉.感 ...

  6. Internet History, Technology, and Security(week1)——History: Dawn of Electronic Computing

    前言: 第一次进行课程学习,在反复观看视频和查找字典翻译理解后选出了视频中个人认为较重要的概念,以下并不按照逐句翻译,中文概括大意余下自由发挥,对老师想要告诉我们的历史有一个初步的了解,顺便锻炼以下英 ...

  7. Java Interger类,两对整数明明完全一样,为何一个输出true,一个输出false

    package text; public class MethodOverload { public static void main(String[] args) { Integer i1=100; ...

  8. [CSP-S模拟测试]:小P的生成树(数学+Kruskal)

    题目描述 小$P$是个勤于思考的好孩子,自从学习了最大生成树后,他就一直在想:能否将边权范围从实数推广到复数呢?可是马上小$P$就发现了问题,复数之间的大小关系并没有定义.于是对于任意两个复数$z_1 ...

  9. SQL 基本查询语句

    --使用数据库 use date go --创建表班级表 create table classInfo ( classNo ,),--主键约束使用primary key identity classN ...

  10. 安装完Fedora 18后需要做的事情

    折腾了好久,在网上查看了好多资料,总算吧安装好的Fedora 18配置得差不多了,现在将过程记录下来,供以后查看用,同时也许还能帮助到和我遇到同一问题的朋友们,以后再有什么再继续添加吧. 一.添加 y ...