Max Sum Plus Plus

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

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.

 
Author
JGShining(极光炫影)
 
dp[i][j]表示前j个数组成的序列最大i子段和,则状态转移方程为
dp[i][j]=max{dp[i][j-1]+a[j],max{dp[i-1][t]}+a[j]} i-1=<t<j-1
优化1:本题数据范围太大, 所以可以用滚动数组优化
优化2:用last数组记录上一层循环得到的最大值,具体见代码
/*
ID: LinKArftc
PROG: 1024.cpp
LANG: C++
*/ #include <map>
#include <set>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <cstdio>
#include <string>
#include <climits>
#include <utility>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define eps 1e-8
#define randin srand((unsigned int)time(NULL))
#define input freopen("input.txt","r",stdin)
#define debug(s) cout << "s = " << s << endl;
#define outstars cout << "*************" << endl;
const double PI = acos(-1.0);
const double e = exp(1.0);
const int inf = 0x3f3f3f3f;
const int INF = 0x7fffffff;
typedef long long ll; const int maxn = ; int num[maxn], dp[maxn], last[maxn]; int main() {
int n, m;
while (~scanf("%d %d", &m, &n)) {
memset(dp, , sizeof(dp));
memset(last, , sizeof(last));
for (int i = ; i <= n; i ++) scanf("%d", &num[i]);
int ma;
for (int i = ; i <= m; i ++) {//分成i段
ma = INT_MIN;
for (int j = i; j <= n; j ++) {//前j个数
dp[j] = max(dp[j-], last[j-]) + num[j];//dp[j]表示前j个划分成i段最大值,last[j-1]表示前j-1个划分成i-1段最大值
last[j-] = ma;
ma = max(ma, dp[j]);
}
last[n] = ma;
}
printf("%d\n", ma);
} return ;
}

HDU1024(最大M子段和)的更多相关文章

  1. HDU1024 最大m子段和

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

  2. HDU1024 最大M子段和问题 (单调队列优化)

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

  3. 【题解】最大 M 子段和 Max Sum Plus Plus [Hdu1024] [51nod1052]

    [题解]最大 M 子段和 Max Sum Plus Plus [Hdu1024] [51nod1052] 传送门:最大 \(M\) 子段和 \(Max\) \(Sum\) \(Plus\) \(Plu ...

  4. HDU1024 DP的优化 最大M子段和问题

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

  5. m大子段和 hdu1024

    给出n个数,m个区间: 求选区m个区间的最大值: #include<cstdio> #include<algorithm> #include<math.h> #in ...

  6. 最大m段子段和

    hdu1024 最大m子序列和 给定你一个序列,让你求取m个子段(不想交的子段)并求取这m个子段和的最大值 从二维开始来看dp[i][j]表示取第j个数作为第i个子段的元素所得到的前i个子段和的最大值 ...

  7. 最大子段和(c++)

    // 最大子段和.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include<iostream> using namesp ...

  8. hdu1024 dp

    题意:求一个序列中的最大 m 段和,m 段不能交叉. dp[i][0/1][j] 表示已经取完第 i 个物品,第 i 个物品取或不取,取到第 j 个子段. 用vis[i][0/1][j] 表示该 dp ...

  9. 51Node 1065----最小正子段和

    51Node  1065----最小正子段和 N个整数组成的序列a[1],a[2],a[3],…,a[n],从中选出一个子序列(a[i],a[i+1],…a[j]),使这个子序列的和>0,并且这 ...

随机推荐

  1. Gradle下载依赖jar包位置修改

    gradle会下载相关需要依赖的jar包,默认的本地存放地址是:C:/Users/(用户名)/.gradle/caches/modules-2/files-2.1,很多人和我一样不愿意放在C盘,所以需 ...

  2. 【性能监控】虚拟内存监控命令vmstat详解

    一.Vmstat说明 vmstat是Virtual Meomory Statistics(虚拟内存统计)的缩写,可对操作系统的虚拟内存.进程.CPU活动进行监控.vmstat 工具提供了一种低开销的系 ...

  3. Python 3 学习笔记之——变量作用域、模块和包

    1. 变量作用域 Python 中,程序的变量并不是在哪个位置都可以访问的,访问权限决定于这个变量是在哪里赋值的.变量的作用域决定了在哪一部分程序可以访问哪个特定的变量名称.Python 的作用域一共 ...

  4. GCD LCM 最大公约数 最小公倍数 分数模板 (防溢出优化完成)

    自己写的一个分数模板,在运算操作时进行了防溢出的优化: ll gcd(ll a, ll b) { return b ? gcd(b, a%b) : a; } ll lcm(ll a, ll b) { ...

  5. Windows server 2012 R2开机进入cmd,关闭后黑屏问题

    原因分析: 因为自己在卸载IIS的时候,不小心卸载了.net framework,系统没有了图形界面(由完整模式Full变为了核心模式core),需要重新恢复.net framework4.5. 解决 ...

  6. AM5728通过GPMC接口与FPGA高速数据通信实现

    硬件:AM5728开发板:Artix-7开发板软件:Linux am57xx-evm 4.4.19:Vivado 2015.2作者:杭州矢志信息科技有限公司邮箱:admin@sysjoint.com ...

  7. struts2 action中获取request session application的方法

    共四种方式: 其中前两种得到的是Map<String,Object>  后两种得到的才是真正的request对象 而Map就是把request对象中的属性取出做成了键值对而已. [方法一] ...

  8. 走进Android系统

    一.Android背景 [Android定义] Android是Google公司在2007年11月5日公布的基于Linux平台的开源手机操作系统. [发展历程] 2005年,Google收购企业And ...

  9. 福大软工1816:Alpha(3/10)

    Alpha 冲刺 (3/10) 队名:第三视角 组长博客链接 本次作业链接 团队部分 团队燃尽图 工作情况汇报 张扬(组长) 过去两天完成了哪些任务: 文字/口头描述: 1.学习qqbot库: 2.实 ...

  10. 基于SDN的IP RAN网络虚拟化技术

    http://www.zte.com.cn/cndata/magazine/zte_technologies/2014/2014_4/magazine/201404/t20140421_422858. ...