hdu 1024 Max Sum Plus Plus
Max Sum Plus Plus
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 25639 Accepted Submission(s): 8884
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. ^_^
Process to the end of file.
2 6 -1 4 -2 3 -2 3
8
Huge input, scanf and dynamic programming is recommended.
#include<iostream>
#include<stdio.h>
#include<string>
#include<cstring>
using namespace std;
const int maxn = ;
int dp[maxn];
int pri[maxn];
int a[maxn]; int main()
{
int n,m;
while(~scanf("%d%d",&m,&n))
{
for(int i=;i<=n;i++) scanf("%d",a+i);
int tmp_max=-0x3fff;
dp[]=;
//pri[0]=0;
memset(pri,,sizeof(pri));
for(int i=;i<=m;i++)
{
tmp_max=-0x3fffffff;
for(int j=i;j<=n;j++)
{
dp[j]=max(dp[j-],pri[j-])+a[j];
pri[j-]=tmp_max;
if(tmp_max<dp[j]) tmp_max=dp[j];
} }
printf("%d\n",tmp_max);
}
return ;
}
设输入的数组为a[1...n],从中找出m个段,使者几个段的和为最大
dp[i][j]表示前j个数中取i个段的和的最大值,其中最后一个段包含a[j]。(这很关键)
则状态转移方程为:
dp[i][j]=max{dp[i][j-1]+a[j],max{dp[i-1][t]}+a[j]} i-1=<t<j-1
因为dp[i][j]中a[j]可能就自身一个数组成最后一段,或者a[j]与a[j-1]等前面的数组成最后一段。
此题n数据太大,二维数组开不下,而且三重循环,想到状态转移方程后还是困难重重。
想想,二维数组不行的话,肯定要压缩成一维数组:
因为dp[i-1][t]的值只在计算dp[i][j]的时候用到,那么没有必要保存所有的dp[i][j] for i=1 to m,这样我们可以用一维数组存储。
用pre[j]表示j之前一个状态dp[i-1][]中1-j之间,不一定包含a[j]的最大字段和,然后推dp[i][j]状态时,dp[i][j]=max{pre[j-1],dp[j-1]}+a[j];
褐色的为了方便理解,其实不存在。
hdu 1024 Max Sum Plus Plus的更多相关文章
- HDU 1024 Max Sum Plus Plus --- dp+滚动数组
HDU 1024 题目大意:给定m和n以及n个数,求n个数的m个连续子系列的最大值,要求子序列不想交. 解题思路:<1>动态规划,定义状态dp[i][j]表示序列前j个数的i段子序列的值, ...
- HDU 1024 Max Sum Plus Plus (动态规划)
HDU 1024 Max Sum Plus Plus (动态规划) Description Now I think you have got an AC in Ignatius.L's "M ...
- HDU 1024 Max Sum Plus Plus(m个子段的最大子段和)
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1024 Max Sum Plus Plus Time Limit: 2000/1000 MS (Java/ ...
- HDU 1024 max sum plus
A - Max Sum Plus Plus Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I6 ...
- HDU 1024 Max Sum Plus Plus【动态规划求最大M子段和详解 】
Max Sum Plus Plus Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- hdu 1024 Max Sum Plus Plus DP
Max Sum Plus Plus Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php ...
- 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 ...
- HDU 1024 Max Sum Plus Plus(DP的简单优化)
Problem Description Now I think you have got an AC in Ignatius.L's "Max Sum" problem. To b ...
- hdu 1024 Max Sum Plus Plus(m段最大和)
Problem Description Now I think you have got an AC in Ignatius.L's "Max Sum" problem. To b ...
随机推荐
- Java for LeetCode 210 Course Schedule II
There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...
- C# 操作IIS方法集合
如果在win8,win7情况下报错:未知错误(0x80005000) 见http://blog.csdn.net/ts1030746080/article/details/8741399 using ...
- C#集合及特殊字符
集合里面 打印 出来时 要把 集合内的格式转化为其他格式! 壹. 集合 在.add之前 为空 数组 同样 添加元素之前 为 空(下一章超市购物例题具体体现) 1.System Collec ...
- JavaScript for循环里边异步操作问题。
问题:(DRIVING.search是异步操作) for循环中做异步操作会导致aDistances数组里边的数据全部都是从A_SHOP_INFO数组中最后一条数据获取的值. var iIdx = 0; ...
- ios 中直接修改frame里边某个属性的简便方法
参考:http://www.cnblogs.com/wengzilin/p/4359865.html 在iOS中view的frame属性使用地太频繁了,尤其是调UI的时候.我们知道,正常情况下我们无法 ...
- August 23rd 2016 Week 35th Tuesday
The very essence of romance is uncertainty. 浪漫的精髓就在于它充满种种可能. And the uncertainty of life may be also ...
- SQL Server 数据类型
数据类型的选择帮助优化查询,比如针对int类型列和针对文本类型列可能会生成完全不同的查询计划 三种数据类型: 系统数据类型 别名数据类型:用户可以为系统数据类型提供一个别名,并且可以对数据类型做进一步 ...
- JPush集成
JPush SDK 收到推送,通过广播的方式,转发给开发者App,这样开发者就可以灵活地进行处理. 这个动作不是必须的.用户有需要才定义 Receiver 类来处理 SDK过来的广播. 如果不做这个动 ...
- iOS - 线程管理
iOS开发多线程篇—GCD的常见用法 一.延迟执行 1.介绍 iOS常见的延时执行有2种方式 (1)调用NSObject的方法 [self performSelector:@selector(run) ...
- Ubuntu下调整swap分区的大小
转自:http://blog.chinaunix.net/uid-7573623-id-2048964.html 由于安装oracle 的时候,swap太小不能继续安装,于是想有什么方法在不不用安装o ...