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. RDLC 表达式设置精度

    =IIf(RTrim(Parameters!u_currency.Value)="VND","F0","F2")

  2. 3.java发展简史

    1991年,James Gosling在SUN(Stanford University Network)公司的工程师小组想要设计这样一种小型计算机语言.该语言主要用于像电视盒这样的消费类电子产品.另外 ...

  3. vue父孙组件传值($attr及$listeners)的使用

    父组件 <template> <div> <!-- 将值传给子组件 子组件可以获取自己想要的值 也可以忽视掉需要传给孙子组件的值 --> <!-- 同时获取通 ...

  4. LoadRunner的Capture Level

    场景:录制loadrunner的web站点 问题:录制的过程中发现除了页面正常的请求外还有些看不懂的请求(包含乱码),脚本如下: web_url("WebTours", " ...

  5. 并发编程之GIL

    目录 GIL 什么是GIL锁 为什么需要加锁 带来的问题 如何解决 关于性能的讨论 计算密集型任务:进程执行更快 IO密集型:线程执行更快 自定义锁与GIL的区别 GIL 什么是GIL锁 官方解释: ...

  6. if,while,for循环

    目录 if条件 while循环 for循环 拓展知识点 if条件 if 条件: code elif 条件: code else: code # 三元运算符 x = 10 y = 20 print(y ...

  7. tomcat注册为windows服务

    打开CMD,进入到Tomcat的bin目录,执行命令:service.bat install  [service_name] 如果卸载服务,可以执行:sc delete [service_name]

  8. gensim加载词向量文件

    # -*- coding: utf-8 -*- # author: huihui # date: 2020/1/31 7:58 下午 ''' 根据语料训练词向量,并保存向量文件 ''' import ...

  9. c数据结构线性表的总结

    1:连表的特点   1.链表是一种存储结构,用于存放线性表   2.连表的结点是根据需要调用动态分配内存函数进行分配的,因此链表可随需要伸长缩短      在腰存储的数据个数未知的情况下节省内存.   ...

  10. Debug运行项目时报错,connected to the target VM, address: '127.0.0.1:50495', transport: 'socket'

    Debug运行项目时报错,无法进入Debug,猜想出错原因可能是未正确关闭IDEA. 解决方法,先直接运行项目,然后停掉项目,再用Debug模式启动,问题解决.