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)
Total Submission(s): 21336 Accepted Submission(s): 7130
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. ^_^
Process to the end of file.
1 3 1 2 3
2 6 -1 4 -2 3 -2 3
6
8HintHuge 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子段和的更多相关文章
- 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 ...
- 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 ...
- 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 ...
- HDU 1024 Max Sum Plus Plus【DP,最大m子段和】
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1024 题意: 给定序列,给定m,求m个子段的最大和. 分析: 设dp[i][j]为以第j个元素结尾的 ...
- HDU 1024 Max Sum Plus Plus --- dp+滚动数组
HDU 1024 题目大意:给定m和n以及n个数,求n个数的m个连续子系列的最大值,要求子序列不想交. 解题思路:<1>动态规划,定义状态dp[i][j]表示序列前j个数的i段子序列的值, ...
- 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/ ...
- 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 ...
- hdu 1024 Max Sum Plus Plus (动态规划)
Max Sum Plus PlusTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- 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 ...
随机推荐
- LVS负载均衡软件使用及(LVS简介、三种工作模式、十种调度算法)
一.LVS简介 LVS(Linux Virtual Server)即Linux虚拟服务器,目前LVS已经被集成到Linux内核模块中.该项目在Linux内核中实现了基于IP的数据请求负载均衡调度方案, ...
- mysql之指定为definer的用户不存在
问题描述: java.sql.SQLException: The user specified as a definer ('tsingsoft'@'%') does not exist 解决: 1. ...
- SyntaxError: (unicode error) 'utf-8' codec can't decode byte 0xd0 in position 2: invalid continuation byte
[root@hostuser src]# python3 subprocess_popen.py File "subprocess_popen.py", line 23Syntax ...
- New Airless Pump Bottle Technical Features
Airless Pump Bottle protect sensitive products such as natural skin creams, serums, foundations a ...
- input、raw_input区别,运算符,运算优先级,多变赋值方式
目录 1. Python2中的input.raw_input赋值方式和Python3中的input赋值方式的差别 2. 运算符 3. python运算符优先级 4. 格式化输出 5. 链式赋值 6. ...
- 吴裕雄 Bootstrap 前端框架开发——Bootstrap 表格:为所有表格的单元格添加边框
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- flex布局(非常重要)
首先明确一点是, flex 是 flex-grow.flex-shrink.flex-basis的缩写.故其取值可以考虑以下情况: flex 的默认值是以上三个属性值的组合.假设以上三个属性同样取默认 ...
- StringUtils工具类中的isBlank()方法和isEmpty()方法的区别
1.isBlank()方法 1 public static boolean isBlank(String str) { 2 int strLen; 3 if (str == null || (strL ...
- Jsp和Servlet关系
为什么会出现Jsp? 其实对于服务器来说它只认识Servlet,我们完全可以在Servlet用resp.getWriter().write("");画出网页的界面,但是仅仅一个很简 ...
- Tensorflow之AttributeError: module 'tensorflow' has no attribute 'mul'
tf.mul已经在新版本中被移除,请使用 tf.multiply 代替