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. Qt 汽车仪表 QWidget

    今天是2016年的最后一个工作日,在这个最后工作日里面,和以为网友要了一点练手的素材文件,经过网友确认,不涉及商业机密,在这里分享一下,如侵权,请联系我删除. 先上程序运行图 这里显示数字,闪烁等都没 ...

  2. 接口测试工具postman(四)导入导出文件

    1.导入json文件 2.单个文件夹导出,文件格式是 json文件 3.所有数据导出,文件格式是 json文件

  3. 9.0 toast定位+WebDriverWait显示等待

    Toast  判断-----基本操作问题 首先基本操作,进入安卓市场的账号密码页面--- from appium import webdriver from selenium.webdriver.su ...

  4. Assetbundle2

    Assetbundle可以将Prefab封装起来,这是多么方便啊! 而且我也强烈建议大家将Prefab封装成Assetbundle,因为Prefab可以将游戏对象身上带的游戏游戏组件.游戏脚本.材质都 ...

  5. CSS3 : box-shadow

    box-shadow 用于设置边框阴影,语法: box-shadow: h-shadow v-shadow blur spread color inset; -moz-box-shadow: h-sh ...

  6. ubuntu中 VI 方向键、删除键问题

    这两天重新装的ubuntu系统,发觉使用VI时,方向键按下去后变成ABCD,删除键无效.网上搜寻一番,应该是VI软件本身的问题,顾卸载重装即可,步骤如下: 1.执行命令 sudo apt-get re ...

  7. QR码与DM码的区别

    DM无法表现汉字等其他形式,而QR码能用数据压缩方式来表示汉字,仅用13bit即可表示一个汉字,比其他二维条码表示汉字的效率提高了20%.相较而言,DM码信息容量小,应用简单.而QR在汉字处理上更有优 ...

  8. 腾讯云,搭建 FTP 文件服务

    腾讯云,搭建 FTP 文件服务 腾讯云,搭建 FTP 文件服务 安装并启动 FTP 服务 任务时间:5min ~ 10min 安装 VSFTPD 使用 yum 安装 vsftpd: yum insta ...

  9. DPDK vhost库

    原创翻译,转载请注明出处. vhost库实现了一个用户空间的virtio net server,允许用户直接处理virtio ring队列.换句话说,它让用户可以从VM virtio网络设备读取或写入 ...

  10. javascript中将整数添加千位符号

    如果num是整数的话,将其转换成带千位符号的字符串: Number(num).toString().replace(/(\d)(?=(\d{3})+(?!\d))/g, '$1' +  ','); 另 ...