Max Sum Plus Plus

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 21336    Accepted Submission(s): 7130

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.

具体解释见代码:
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <cstring>
#pragma warning(disable:4996)
using namespace std; #define maxn 1000002
#define minn -1*(1e9+7) int n, m;
int dp[maxn], b[maxn], val[maxn]; int main()
{
//freopen("i.txt","r",stdin);
//freopen("o.txt","w",stdout); int i, j;
int res;
while (scanf("%d%d", &m, &n) != EOF)
{
for (i = 1; i <= n; i++)
{
scanf("%d", val + i);
}
memset(dp, 0, sizeof(dp));
memset(b, 0, sizeof(b)); //dp[i][j]表示i个数分为j组且在选取了第i个数的前提下的最大值
//dp[i][j]=max(dp[i-1][j]+a[j],max(dp[0][j-1]~dp[i-1][j-1])+a[j])
//dp[x]表示第i轮的dp[x][i],即表示x个数时分成i个组的最大值
//b[x]表示上一轮所有的最大值,即第j轮时,b[x]=max(dp[0][j-1]~dp[x-1][j-1])
for (j = 1; j <= m; j++)
{
res = minn;
for (i = j; i <= n; i++)
{
//表示dp[j][i]只有两种可能来源,一个是dp[j-1][i]+val[j],一个是max(dp[0][j-1]~dp[i-1][j-1])+a[j]
dp[i] = max(dp[i - 1] + val[i], b[i - 1] + val[i]);
b[i - 1] = res;
res = max(res, dp[i]);
}
}
printf("%d\n", res);
}
//system("pause");
return 0;
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

HDU 1024:Max Sum Plus Plus 经典动态规划之最大M子段和的更多相关文章

  1. 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 ...

  2. 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 ...

  3. 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 ...

  4. HDU 1024 Max Sum Plus Plus【DP,最大m子段和】

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1024 题意: 给定序列,给定m,求m个子段的最大和. 分析: 设dp[i][j]为以第j个元素结尾的 ...

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

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

  6. 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/ ...

  7. HDU 1024 Max Sum Plus Plus [动态规划+m子段和的最大值]

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

  8. hdu 1024 Max Sum Plus Plus (动态规划)

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

  9. HDU 1024 Max Sum Plus Plus (动态规划 最大M字段和)

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

随机推荐

  1. redis requires Ruby version >= 2.2.2 系统默认 ruby 版本过低,导致 Redis 接口安装失败

    安装 Redis 接口时异常 ,系统 ruby 版本过低 ! 输入命令 " gem install redis " 出现 " ERROR:  Error installi ...

  2. 浅谈CVE-2018-12613文件包含/buuojHCTF2018签到题Writeup

    文件包含 蒻姬我最开始接触这个 是一道buuoj的web签到题 进入靶机,查看源代码 <!DOCTYPE html> <html lang="en"> &l ...

  3. 同一台服务器lnmpa环境下配置ip或域名访问不同站点

    1.配置域名访问 (1)添加虚拟主机 (2)nginx配置 cd /usr/local/nginx/conf/vhost vim zkadmin.zouke.com.conf (3)apache配置 ...

  4. druid监控sql完整版

    利用Druid实现应用和SQL监控 一.关于Druid Druid是一个JDBC组件,它包括三部分: DruidDriver 代理Driver,能够提供基于Filter-Chain模式的插件体系. D ...

  5. Redis的人门以及使用

    1.Redis的安装 1.1centos下安装Redis 1.1.1 安装gcc 1.1.2 安装过程  图一 图三 2.Redis的启动 2.1 前端模式启动(不推荐) 截图 2.2 后端模式(推荐 ...

  6. 十四 OGNL的概述

    1 什么是OGNL? 对象图导航语言(对象图,以任意一个对象为根,通过OGNL;可以访问与这个对象关联的其他对象).比EL表达式强大很多倍. 可以存取对象的任意属性.调用对象的方法,遍历整个对象的结构 ...

  7. Linux系统的发展历史和学习前景介绍

    2020年了,我想来跟大家聊聊Linux运维这一行业,从几个方面说下行业的现状.如何学好Linux和如何成为专业运维人员以及云服务对于Linux运维的影响. 一.linux行业状况 我们都知道从199 ...

  8. RESTFul Client入门实例

    client.html文件内容为: <!DOCTYPE html> <html> <head> <title>RESTFul Client test p ...

  9. c3p0 获取数据源

    getDataSourcec3p0Resource private static void f3Resource() throws Exception { Connection conn = getD ...

  10. Python 基础之面向对象之常用魔术方法

    一.__init__魔术属性 触发时机:实例化对象,初始化的时候触发功能:为对象添加成员,用来做初始化的参数:参数不固定,至少一个self参数返回值:无 1.基本用法 #例:class MyClass ...