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. MongoDB 初始化数据同步

    MongoDB初始化数据同步: 副本集中的成员启动之后,就会检查自身的状态,确定是否可以从某个成员那里进行同步.如果不行的话,尝试从其他成员那里进行完整的数据复制. 这个过程就是初始化同步(initi ...

  2. jQuery省市联动(XML/JSON)

    准备: 导包 在src下导入c3p0-config.xml 导入JDBCUtil 创建数据库 新建js文件夹导入jQuery配置文件 NO01:创建city.jsp页面 <%@ page lan ...

  3. 001、在本地搭建SAP虚拟机环境,用于各种暴力操作

    一.在某网盘下载一个SAP虚拟机,用于SAP学习和相关的测试.打开图中的服务器,点击运行,等灯都变成绿色 二.点击打开熟悉的SAP登录图标 三.很完美的运行起来了. 友情提示:SAP对电脑配置要求挺高 ...

  4. 030-PHP日期查询函数

    <?php , , ))//检查日期函数 { print("2,18,1970 :" . "这是一个正确的日期格式"); } else { print(& ...

  5. 161-PHP 文本替换函数str_replace(二)

    <?php $str='Hello world!'; //定义源字符串 $search='o'; //定义将被替换的字符 $replace='O'; //定义替换的字符串 $res=str_re ...

  6. JVM学习与问题总结——java内存区域与内存溢出异常

    java虚拟机将内存分为哪些区域? 根据Java SE7版本的Java虚拟机规范,虚拟机管理的内存包括5个运行时数据区域: 程序计数器 虚拟机栈 本地方法栈 方法区 堆 运行时数据区各部分的作用? 程 ...

  7. Spark 调优

    资源调优 (1). 在部署 spark 集群中指定资源分配的默认参数 在 spark 安装包的 conf 下的 spark-env.sh SPARK_WORKER_CORES SPARK_WORKER ...

  8. 14 ~ express ~ 显示用户数据

    一,router/admin.js var express = require('express') var router = express.Router() var User = require( ...

  9. bugku-Web-多次(异或注入,判断被过滤的关键字)

    进去看到url感觉是sql注入, 加上',报错但是%23不报错,加上'--+,也不报错,说明可以用--+注释 加上' or 1=1--+,报错 尝试' oorr 1=1--+,正常 说明or被过滤了. ...

  10. Apache Spark:来自Facebook的60 TB +生产用例

    本文主要讲Facebook扩展spark替换hive的过程中积累的经验和教训. 浪尖整理翻译https://databricks.com/blog/2016/08/31/apache-spark-sc ...