DP———2.最大m子序列和
Max Sum Plus Plus
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 35485 Accepted Submission(s): 12639
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.
2 6 -1 4 -2 3 -2 3
8
Huge input, scanf and dynamic programming is recommended.
状态dp[i][j]有前j个数,组成i组的和的最大值。决策:
第j个数,是在第包含在第i组里面,还是自己独立成组。
方程 dp[i][j]=Max(dp[i][j-1]+a[j] , max( dp[i-1][k] ) + a[j] ) 0<k<j
空间复杂度,m未知,n<=1000000, 继续滚动数组。
时间复杂度 n^3. n<=1000000. 显然会超时,继续优化。
max( dp[i-1][k] ) 就是上一组 0....j-1 的最大值。
我们可以在每次计算dp[i][j]的时候记录下前j个的最大值
用数组保存下来 下次计算的时候可以用,这样时间复杂度为 n^2.
#include <cstdio>
#include <algorithm>
#include <iostream>
#include <cstring>
using namespace std;
const int maxn = 1e6+;
const int INF = 0x7fffffff;
int dp[maxn];
int a[maxn];
int mmax[maxn];
int main(){
int n,m;
int maxx;
while(scanf("%d%d",&m,&n) !=EOF){
for(int i=;i<=n;i++){
scanf("%d",&a[i]);
mmax[i]=;
dp[i]=;
}
dp[]=;
mmax[]=;
for(int i=;i<=m;i++){
maxx=-*INF;
for(int j=i;j<=n;j++){
dp[j]=max(dp[j-]+a[j],mmax[j-]+a[j]);
mmax[j-]=maxx;
maxx=max(maxx,dp[j]);
}
}
printf("%d\n", maxx);
}
return ;
}
DP———2.最大m子序列和的更多相关文章
- HDU 1087 简单dp,求递增子序列使和最大
Super Jumping! Jumping! Jumping! Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 ...
- hdu 1025 dp 最长上升子序列
//Accepted 4372 KB 140 ms //dp 最长上升子序列 nlogn #include <cstdio> #include <cstring> #inclu ...
- DP——最长上升子序列(LIS)
DP——最长上升子序列(LIS) 基本定义: 一个序列中最长的单调递增的子序列,字符子序列指的是字符串中不一定连续但先后顺序一致的n个字符,即可以去掉字符串中的部分字符,但不可改变其前后顺序. LIS ...
- 动态规划(Dynamic Programming, DP)---- 最大连续子序列和
动态规划(Dynamic Programming, DP)是一种用来解决一类最优化问题的算法思想,简单来使,动态规划是将一个复杂的问题分解成若干个子问题,或者说若干个阶段,下一个阶段通过上一个阶段的结 ...
- [ An Ac a Day ^_^ ] HDU 1257 基础dp 最长上升子序列
最近两天在迎新 看来只能接着水题了…… 新生培训的任务分配 作为一个有担当的学长 自觉去选了动态规划…… 然后我觉得我可以开始水动态规划了…… 今天水一发最长上升子序列…… kuangbin有nlog ...
- 洛谷 P1020 导弹拦截(dp+最长上升子序列变形)
传送门:Problem 1020 https://www.cnblogs.com/violet-acmer/p/9852294.html 讲解此题前,先谈谈何为最长上升子序列,以及求法: 一.相关概念 ...
- hdu1159 dp(最长公共子序列)
题意:给两个字符串,求这两个字符串的最长公共子序列的长度 因为之前集训的时候做过,所以现在即使会做也并不是什么稀奇的事,依旧为了自己的浅薄感到羞愧啊``` 解法就是通过两个字符串的每个字符互相比较,根 ...
- POJ-1887 Testing the CATCHER(dp,最长下降子序列)
Testing the CATCHER Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 16515 Accepted: 6082 ...
- hdu1087 dp(最大上升子序列和)
题意,给出一列数,要求所有上升子序列中序列和最大的. 这回不是求长度了,但是还是相当基础的 dp 水题,只要用 dp [ q ] 记录以 第 q 个数 a [ q ] 为结尾的上升子序列的最大的和就可 ...
- HDU 4632 Palindrome subsequence(区间DP求回文子序列数)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4632 题目大意:给你若干个字符串,回答每个字符串有多少个回文子序列(可以不连续的子串).解题思路: 设 ...
随机推荐
- python 获取项目的根路径
root_path = os.path.abspath(os.path.dirname(__file__)).split('shippingSchedule')[0] shippingSchedule ...
- 读键值对封装成Map
描述: 有配置文件address_relation.properties,记录地址关系,有如下数据:ZSSS=ZS%,ZSPD, 封装到Map代码如下: public static void main ...
- 原生js关闭窗口
if (navigator.userAgent.indexOf("MSIE") > 0) { if (navigator.userAgent.indexOf("MS ...
- nuxt.config有关router配置
这里只说明一个属性,其他属性移步官方文档 https://zh.nuxtjs.org/api/configuration-router extendRoutes 官方说明: 你可以通过 exten ...
- 用PHP关于Jquery表单插件ajaxForm里success不返回问题
简单说一下吧,在用ajaxForm的时候,sucess突然之间不返回了,直接转到error里面去, 网页代码 ................. $('#add-type').ajaxForm({ d ...
- 实用jquery插件
jquery toast 一个弹窗插件 jquery.form.min.js 一个Ajax表单插件
- python -- 输出异常详细信息
在使用try: except: 捕获异常后,想要获取到异常信息的详细内容另做它用,可以使用python的内置模块traceback进行获取. traceback.print_exc() 直接打印异 ...
- Drazil and Tiles CodeForces - 516B (类拓扑)
Drazil created a following problem about putting 1 × 2 tiles into an n × m grid: "There is a gr ...
- PHP.14-图片处理类
图片处理类 test.php <?php include "images.class.php"; $image=new Image("./images/" ...
- HTML中body相关标签-02
今日内容: 字体标签: h1~h6.<font>.<u>.<b>.<strong><em>.<sup>.<sub> ...