HDU 1024:Max Sum Plus Plus(DP,最大m子段和)
Max Sum Plus Plus
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 36673 Accepted Submission(s): 13069
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.
题意
给出一个长度为n的数组,求数组中m个不相交的子段的最大和
参考了kuangbin大神的博客还有好多大佬们的博客才算勉强弄懂了。
状态转移方程为:dp[i][j]=max(dp[i][j-1]+a[j],dp[i-1][k]+a[j]) i-1<=k<=j-1
但是需要对这个方程进行优化,具体优化过程看下面各位大佬们的博客吧
传送门
https://www.cnblogs.com/jiangjing/archive/2013/07/25/3214729.html
https://blog.csdn.net/u013187393/article/details/42914165
代码
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <math.h>
#include <limits.h>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#include <set>
#include <string>
#define ll long long
#define ms(a) memset(a,0,sizeof(a))
#define pi acos(-1.0)
#define INF 0x3f3f3f3f
const double E=exp(1);
const int maxn=1e6+10;
using namespace std;
int a[maxn];
int dp[maxn];//dp[j]表示到第j个时候的最大和
int sum[maxn];//记录上个状态的j-1个前的最大值
int main(int argc, char const *argv[])
{
ios::sync_with_stdio(false);
int k,n;
while(cin>>k>>n)
{
ms(a);
ms(dp);
ms(sum);
for(int i=1;i<=n;i++)
cin>>a[i];
int ans;
for(int i=1;i<=k;i++)
{
ans=INT_MIN;
for(int j=i;j<=n;j++)
{
//当前位置的最大和
dp[j]=max(dp[j-1]+a[j],sum[j-1]+a[j]);
//记录上个状态的最大和
sum[j-1]=ans;
ans=max(ans,dp[j]);
}
}
cout<<ans<<endl;
}
return 0;
}
HDU 1024:Max Sum Plus Plus(DP,最大m子段和)的更多相关文章
- 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 DP
Max Sum Plus Plus Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php ...
- HDU - 1024 Max Sum Plus Plus 最大m段子段和+滚动数组优化
给定n个数字,求其中m段的最大值(段与段之间不用连续,但是一段中要连续) 例如:2 5 1 -2 2 3 -1五个数字中选2个,选择1和2 3这两段. dp[i][j]从前j个数字中选择i段,然后根据 ...
- 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】
Now I think you have got an AC in Ignatius.L's "Max Sum" problem. To be a brave ACMer, we ...
- 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 ...
- HDU 1024 Max Sum Plus Plus(基础dp)
Max Sum Plus Plus Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- HDU 1024 max sum plus
A - Max Sum Plus Plus Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I6 ...
- 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 ...
随机推荐
- Maven置标签<scope>
在POM 4中,<dependency>中引入了<scope>,它主要管理依赖的部署.目前<scope>可以使用5个值: * compile,缺省值,适用于所有阶段 ...
- asp.net一般处理程序利用反射定位方法
asp.net的一般处理程序我想大家用得都不少,经常会如下如下的代码: using System; using System.Collections.Generic; using System.Lin ...
- ASP.NET调用dos命令获取交换机流量
protected void btn_Cisco_Click(object sender, EventArgs e) { try { string ip = txt_ip.Value; string ...
- Mysql错误: Lock wait timeout exceeded 解决办法
一.临时解决办法: 执行mysql命令:show full processlist; 然后找出插入语句的系统id 执行mysql命令:kill id 或 首先,查看数据库的进程信息: show ful ...
- JVM自定义类加载器加载指定classPath下的所有class及jar
一.JVM中的类加载器类型 从Java虚拟机的角度讲,只有两种不同的类加载器:启动类加载器和其他类加载器. 1.启动类加载器(Boostrap ClassLoader):这个是由c++实现的,主要负责 ...
- python-day10--文件处理
1.文件:是操作系统提供的概念 2. open(r+'文件路径' , '打开方式' , '用什么字符编码') #r 表示原始字符串 eg:open(r'C:\Users\13264\Desktop ...
- hdu5253 MST
连接的管道 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submi ...
- Spring boot 嵌入的tomcat不能启动: Unregistering JMX-exposed beans on shutdown
原因是:没有引入tomcat依赖包 <dependency> <groupId>org.springframework.boot</groupId> <art ...
- 算法笔记_025:字符串的全排列(Java)
目录 1 问题描述 2 解决方案 2.1 递归实现 2.2 字典序排列实现 1 问题描述 输入一个字符串,打印出该字符串的所有排列.例如,输入字符串”abc”,则输出有字符’a’,’b’,’c’所 ...
- dubbo的超时重试
dubbo的超时分为服务端超时 SERVER_TIMEOUT 和客户端超时 CLIENT_TIMEOUT.本文讨论服务端超时的情形: 超时:consumer发送调用请求后,等待服务端的响应,若超过ti ...