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 S
1, S
2, S
3, S
4 ... S
x, ... S
n (1 ≤ x ≤ n ≤ 1,000,000, -32768 ≤ S
x ≤ 32767). We define a function sum(i, j) = S
i + ... + S
j (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(i
1, j
1) + sum(i
2, j
2) + sum(i
3, j
3) + ... + sum(i
m, j
m) maximal (i
x ≤ i
y ≤ j
x or i
x ≤ j
y ≤ j
x 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(i
x, j
x)(1 ≤ x ≤ m) instead. ^_^

InputEach test case will begin with two integers m and n, followed by n integers S
1, S
2, S
3 ... S
n.

Process to the end of file.

OutputOutput 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个子段使其和最大
题解:dp,dp[i][j]表示到a[j]包括a[j]从中去i段的最大值
所以dp[i][j]就分为两种情况:a[j]取或者不取
dp[i%2][k]=max(dp[i%2][k-1],w[k]);
用w[i]记录一定取的情况,又分为两种情况:

1、a[k]作为第i段

2、a[k]加到之前的最大段那里
        w[k]=max(dp[(i-1)%2][k-1],w[k-1])+sum[k]-sum[k-1];

初值:  dp[0][i]               
   if(i==k)dp[i%2][k]=w[k]=sum[k];
具体看代码:
#include<iostream>
#include<string.h>
#include<algorithm>
#include<stdio.h>
#include<queue>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long long ll;
typedef pair<int,int> PII;
#define mod 1000000007
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
//head
#define INF 0x3f3f3f3f
#define N 1000005
int n,m;
int w[N];
int dp[][N];
int sum[N];
int a[N];
int main()
{
ios_base::sync_with_stdio(); cin.tie();
while(cin>>m>>n){
sum[]=;
for(int i=;i<=n;i++)
{
cin>>a[i];
sum[i]=sum[i-]+a[i];
dp[][i]=;
}
for(int i=;i<=m;i++)
{
for(int k=i;k<=n;k++)
{
if(i==k)
dp[i%][k]=w[k]=sum[k];//从k个数中取k段的最大值是前k个数的和
else
{
w[k]=max(dp[(i-)%][k-],w[k-])+sum[k]-sum[k-];//这是一定要取的情况,分为两种:1、a[k]作为第i段,2、a[k]加到之前的最大段那里
dp[i%][k]=max(dp[i%][k-],w[k]);//a[k]取或者不取
}
}
} cout<<dp[m%][n]<<endl;
}
return ; }

参考博客:http://blog.sina.com.cn/s/blog_677a3eb30100jxqa.html

        https://blog.csdn.net/lishuhuakai/article/details/8067474

A - Max Sum Plus Plus (好题&&dp)的更多相关文章

  1. hdu1003 1024 Max Sum&Max Sum Plus Plus【基础dp】

    转载请注明出处,谢谢:http://www.cnblogs.com/KirisameMarisa/p/4302208.html   ---by 墨染之樱花 dp是竞赛中常见的问题,也是我的弱项orz, ...

  2. 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 ...

  3. Max Sum (hdu 1003 简单DP水过)

    Max Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Su ...

  4. hdu 1024 Max Sum Plus Plus(简单dp)

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1024 题意:给定一个数组,求其分成m个不相交子段和的最大值. 这题有点问题其实m挺小的但题目并没有给出 ...

  5. 杭电60题--part 1 HDU1003 Max Sum(DP 动态规划)

    最近想学DP,锻炼思维,记录一下自己踩到的坑,来写一波详细的结题报告,持续更新. 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1003 Problem ...

  6. HDU 1003 Max Sum --- 经典DP

    HDU 1003    相关链接   HDU 1231题解 题目大意:给定序列个数n及n个数,求该序列的最大连续子序列的和,要求输出最大连续子序列的和以及子序列的首位位置 解题思路:经典DP,可以定义 ...

  7. HDU 1003 Max Sum && HDU 1231 最大连续子序列 (DP)

    Max Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Sub ...

  8. HDU 1024 Max Sum Plus Plus 简单DP

    这题的意思就是取m个连续的区间,使它们的和最大,下面就是建立状态转移方程 dp[i][j]表示已经有 i 个区间,最后一个区间的末尾是a[j] 那么dp[i][j]=max(dp[i][j-1]+a[ ...

  9. 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 ...

随机推荐

  1. zabbix3.0自动发现磁盘并监控磁盘IO

    Zabbix 版本:3.0 操作系统:Ubuntu16.04 操作环境,在被监控的主机上安装zabbix agent.安装方式为源码包安装. 简要安装步骤: 参考:https://www.zabbix ...

  2. Linux设置数据库自动备份

    本文为转载,最末端为原地址 以CentOS 7.6系统与Oracle 11g为例: 一.先找到数据库的环境变量 如果是在root账户下,须先登录到数据库所在账户 su oraclecat ~/.bas ...

  3. tensorflow教程:tf.contrib.rnn.DropoutWrapper

    tf.contrib.rnn.DropoutWrapper Defined in tensorflow/python/ops/rnn_cell_impl.py. def __init__(self, ...

  4. [C++] 所有该类的对象共享静态类成员变量

    问:智能指针可以对指针的引用数量进行计数,一个智能指针释放时,别的智能指针怎么知道的? 同一类的对象共享同一变量最简单的方法是静态变量: 不像普通的变量,静态成员变量是被所有类对象共享的,不同的对象可 ...

  5. 牛客小白月赛16 F 小石的妹子 (线段树)

    链接:https://ac.nowcoder.com/acm/contest/949/F来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语言52428 ...

  6. grep正则 以.o结尾的文件

    ls -l | grep *.o 查不出任何东西 . 代表一定有一个任意字符 * 重复零个到无穷多个前一个字符(所以需要前面有字符) 所以应该是 ls -l | grep '.*\.o' .*表示零个 ...

  7. elasticsearch添加访问密码

    1.将x-pack复制到elasticsearch的plugins目录下面 2.启动elasticsearch .bin/elasticsearch & 3.修改指定用户密码 PUT http ...

  8. vue+element-ui 实现分页(根据el-table内容变换的分页)

    官方例子 官方提示: 设置layout,表示需要显示的内容,用逗号分隔,布局元素会依次显示.prev表示上一页,next为下一页,pager表示页码列表,除此以外还提供了jumper和total,si ...

  9. day15 python lambda函数 递归函数 二分法

    day15 python   一.匿名函数 lambda     1.lambda函数 def func(n):                #普通函数, 功能比较简单, 当函数多的时候, 起名也不 ...

  10. 人生苦短_我用Python_dict(字典)_003

    # coding=utf-8 # 字典 {"key":"values"} 无序的 # dict 可以包含任何类型的数据 values可以是任何数据类型 key必 ...