Max Sum Plus Plus

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

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子段和。

代码:

//最大m子段和递推公式:dp[i][j]=max(dp[i][j-1]+num[j],dp[i-1][t]+num[j]) (i<=t<=j-1)
//优化:因为每次都要求一个最大的dp[i-1][t](i<=t<=j-1),可以每次求出来dp[i][j]时保存
//这个值,到下一次时直接用就行了,这样每次就只用到了dp[i][j]和dp[i][j-1],可以省去一维。
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn=;
const int inf=0x7fffffff;
int dp[maxn],fdp[maxn],num[maxn];
int main()
{
int m,n,maxnum;
while(scanf("%d%d",&m,&n)==){
for(int i=;i<=n;i++) scanf("%d",&num[i]);
memset(dp,,sizeof(dp));
memset(fdp,,sizeof(fdp));
for(int i=;i<=m;i++){
maxnum=-inf;
for(int j=i;j<=n;j++){
dp[j]=max(dp[j-]+num[j],fdp[j-]+num[j]);
fdp[j-]=maxnum;
maxnum=max(maxnum,dp[j]);
}
}
printf("%d\n",maxnum);
}
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. 【题解】最大 M 子段和 Max Sum Plus Plus [Hdu1024] [51nod1052]

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

  3. HDU1024(最大M子段和)

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

  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. python常用函数—enumerate()

    enumerate() 对于一个可迭代的(iterable)/可遍历的对象(如列表.字符串),enumerate将其组成一个索引序列,利用它可以同时获得索引和值的元组. 使用拆包,可以单独获得索引和值 ...

  2. 【算法分析】如何理解快慢指针?判断linked list中是否有环、找到环的起始节点位置。以Leetcode 141. Linked List Cycle, 142. Linked List Cycle II 为例Python实现

    引入 快慢指针经常用于链表(linked list)中环(Cycle)相关的问题.LeetCode中对应题目分别是: 141. Linked List Cycle 判断linked list中是否有环 ...

  3. 虚拟机下 rm -rf / 尝试

    环境:虚拟机 系统版本:centOS 5.8.centOS 6.5 1. root权限:rm -rf / 2. root权限:rm -rf /* 测试结果:5.8下执行命令1,2,根目录文件被删除,系 ...

  4. Python中的赋值语法

    Python中复制语法有6种 Basic Form >>>spam = 'spam' Tuple assignment >>>spam, ham = 'spam', ...

  5. Thunder团队第五周 - Scrum会议3

    Scrum会议3 小组名称:Thunder 项目名称:i阅app Scrum Master:王航 工作照片: 苗威同学在拍照,所以不在照片内. 参会成员: 王航(Master):http://www. ...

  6. 软工1816 · Alpha冲刺(3/10)

    团队信息 队名:爸爸饿了 组长博客:here 作业博客:here 组员情况 组员1(组长):王彬 过去两天完成了哪些任务 完成了对laravel框架的一整套机制的了解,对后端的处理流程有全面的认识对整 ...

  7. ACM 第一天

    标签库元素: 队列<queue> FIFO 栈 <stack>  FICO 集合 set 不定长数组  vector 映射 map Maximum Multiple Time ...

  8. iOS-创建UIScrollerView(封装UIScrollerView)

    创建继承于UIView的类WJImageScrollView,代码实现如下: WJImageScrollView.h #import <UIKit/UIKit.h> /**点击图片bloc ...

  9. PowerMock用法[转]

    转:http://agiledon.github.io/blog/2013/11/21/play-trick-with-powermock/ 当我们面对一个遗留系统时,常见的问题是没有测试.正如Mic ...

  10. 【Docker 命令】- rmi命令

    docker rmi : 删除本地一个或多个镜像. 语法 docker rmi [OPTIONS] IMAGE [IMAGE...] OPTIONS说明: -f :强制删除: --no-prune : ...