Max Sum Plus Plus

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 42478    Accepted Submission(s): 15335

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.

 
 
题意:给出m和n,代表着有n个数,然后要你在这个n个数中找出m个连续子序列的最大和
 
思路:这道题难点不在状态方程,关键在于时间和空间的优化
   
   首先分析下状态,影响我们决策的有两个因素   “数值”  “序列数”,所以我们先把状态设置为二维  dp[ i ][ j ],代表着前 j 个数中取  i 段 的最大值
 
   然后分析下状态转移方程,按照思考,我们需要决定的就是  “要不要这个数”  和  “这个数放在哪个子序列比较好”,所以我们可以推出
 
   dp[ i ][ j ] = max ( dp[ i ][ j -1 ], dp [ i-1 ][ x ]  )+num[ j ] 
 
   num[] 就是存放原始序列的数,x 就是 i-1~j-1的某一个数 ( 因为 dp[ i-1] 代表前面已经有i-1段了,所以一定大于 i-1),dp[ i -1][ x] 就代表着前 j 个数取 i-1 段的最大值.
 
   这个方程代表 决定 第 j 个数,是放在第 i 段上,还是 自己单独成为 一段。
 
 
 
   我们从方程中可以知道,决定“当前状态”的只与“前一状态有关”,
   所以可以用滚动数组( 第一次听说的话,可以先去看看01背包的滚动数组优化 )来优化,
 
   而 dp[ i -1][ x]可以用一个一维数组来优化,
   我们知道 它代表着的是 “ 前 j 个数取 i-1 段的最大值 ”  所以 新设置一个一维数组  head[ j ] 代表着”  前 j 个数取 i-1 段的最大值 “
 
   所以我们的方程变成了这样
 
       dp[ j ]= max( dp[ j -1], head[ j - 1])+num[ j ]
 
   还要记得实时更新head[]数组,因为dp[]是滚动的,而head[]也需要滚动
   
    

//#pragma comment(linker, "/STACK:1024000000,1024000000")
//#pragma GCC optimize(2)
//#include<bits/stdc++.h>
#include <algorithm>
#include <iostream>
#include<sstream>
#include<iterator>
#include<cstring>
#include<string>
#include<cstdio>
#include<cctype>
#include<vector>
#include<deque>
#include<queue>
#include<stack>
#include<map>
#include<set>
using namespace std; typedef double dou;
typedef long long ll;
#define pai acos(-1.0)
#define M 1000005
#define inf 0x3f3f3f3f
#define mod 1e18
#define left k<<1
#define right k<<1|1
#define W(a) while(a)
#define ms(a,b) memset(a,b,sizeof(a)) int n, m, ans;
int num[M], head[M], dp[M]; int main() {
ios::sync_with_stdio(false);
while (cin >> m >> n) {
ms(head, ), ms(dp, );
for (int i = ; i <= n; i++) {
cin >> num[i];
}
for (int i = ; i <= m; i++) {
ans = -inf;
for (int j = i; j <= n; j++) {
dp[j] = max(dp[j - ], head[j - ]) + num[j];
head[j - ] = ans;//此时的ans是i-1段时候的最大值
ans = max(dp[j], ans);//此时的ans是i段的时候的最大值
}
}
cout << ans << endl;
}
return ;
}
    

[kuangbin 带你飞] DP专题——HDU - 1024的更多相关文章

  1. kuangbin带你飞dp专题-基础dp

    dp HDU - 1257 最少拦截系统 最长递增子序列 #include<iostream> using namespace std; const int maxn=1e7; int a ...

  2. 「kuangbin带你飞」专题二十 斜率DP

    layout: post title: 「kuangbin带你飞」专题二十 斜率DP author: "luowentaoaa" catalog: true tags: mathj ...

  3. 「kuangbin带你飞」专题二十二 区间DP

    layout: post title: 「kuangbin带你飞」专题二十二 区间DP author: "luowentaoaa" catalog: true tags: - ku ...

  4. 「kuangbin带你飞」专题十二 基础DP

    layout: post title: 「kuangbin带你飞」专题十二 基础DP author: "luowentaoaa" catalog: true tags: mathj ...

  5. 「kuangbin带你飞」专题十七 AC自动机

    layout: post title: 「kuangbin带你飞」专题十七 AC自动机 author: "luowentaoaa" catalog: true tags: - ku ...

  6. 「kuangbin带你飞」专题十九 矩阵

    layout: post title: 「kuangbin带你飞」专题十九 矩阵 author: "luowentaoaa" catalog: true tags: mathjax ...

  7. 「kuangbin带你飞」专题十四 数论基础

    layout: post title: 「kuangbin带你飞」专题十四 数论基础 author: "luowentaoaa" catalog: true tags: mathj ...

  8. 「kuangbin带你飞」专题十八 后缀数组

    layout: post title: 「kuangbin带你飞」专题十八 后缀数组 author: "luowentaoaa" catalog: true tags: - kua ...

  9. 「kuangbin带你飞」专题十五 数位DP

    传送门 A.CodeForces - 55D Beautiful numbers 题意 一个正整数是 漂亮数 ,当且仅当它能够被自身的各非零数字整除.我们不必与之争辩,只需计算给定范围中有多少个漂亮数 ...

随机推荐

  1. Python基础笔记:函数:调用函数、定义函数、函数的参数、递归函数

    一.定义一个求二元一次方程的根的函数 #Sublime Text import math def ee(a,b,c): delta=b*b-4*a*c if delta<0: return 'n ...

  2. Javascript观察者模式(Object.defineProperty、Reflect和Proxy实现)

    什么是观察者模式? 答:在数据发生改变时,对应的处理函数自动执行.函数自动观察数据对象,一旦对象有变化,函数就会自动执行. 参考<原生JavaScript实现观察者模式>(https:// ...

  3. AI 人工智能产业园路口-----dp

    北京市商汤科技开发有限公司建立了新的 AI 人工智能产业园,这个产业园区里有 nn 个路口,由 n - 1n−1 条道路连通.第 ii 条道路连接路口 u_iui​ 和 v_ivi​. 每个路口都布有 ...

  4. python函数-迭代器&生成器

    python函数-迭代器&生成器 一.迭代器 1 可迭代协议 迭代:就是类似for循环,将某个数据集内的数据可以“一个挨着一个取出来” 可迭代协议: ① 协议内容:内部实现__iter__方法 ...

  5. Django实现websocket

    django实现websocket大致上有两种方式,一种channels,一种是dwebsocket.channels依赖于redis,twisted等 一 dwebsocket 1 Django实现 ...

  6. SMPL模型Shape和Pose参数

    两部分 1.Pose参数 2.Shape参数 一 Pose参数 共24个关节点,对应idx从0到23,图中3个小图分别表示zero shape只有idx节点分别绕x/y/z轴旋转. 其中蓝色线表示-p ...

  7. List列表删除值为指定字段

    需要处理一个场景,当值为某一个固定值或者为空的时候,删除列表中的这个值. ;i<list.size();i++){ if(list.get(i).equals("del")) ...

  8. java内存模型(线程,volatile关键字和sychronized关键字)

    volatile关键字 用在多线程,同步变量. 线程为了提高效率,将某成员变量(如A)拷贝了一份(如B),线程中对A的访问其实访问的是B.只在某些动作时才进行A和B的同步.因此存在A和B不一致的情况. ...

  9. Python win32api.keybd_event模拟键盘输入

    win32api.keybd_event 该函数原型:keybd_event(bVk, bScan, dwFlags, dwExtraInfo) 第一个参数:虚拟键码(键盘键码对照表见附录): 第二个 ...

  10. ansible 文本多行替换实例

    将<level='info'> 等全部替换为<level='ERROR'> - name: Before Ansible 2.3, option 'dest', 'destfi ...