Max Sum Plus Plus

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

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(极光炫影)
Recommend
We have carefully selected several similar problems for
you:  1074 1025 1081 1080 1160 
 
 
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1024
 
 
解题思路:
      用动态规划的思路,由部分推出整体,由1个数分成1组推出n个数分成m组,在每次的过程中找出最大值,就能推出整体的最大值;
首先是第一个数分成一组,再是二个数分成一组,再是三个数分成一组........。每次插入一个数有两种选择——1.将新的数插入之前的组中,2.新的数自成一组;
选哪种选择取决于谁的和最大;
1 . dp[ i ][ j ]=dp[ i ][ j-1 ]+num[ i ]; // i 表示要取的组数, j 表示数的数量;
2 . dp[ i ][ j ]=dp[ i-1 ][ k ]+num[ i ]; //   i-1<=k<j  k表示数的数量 ,i-1 表示要取的组数;
每次不断向dp里加数,因为每次都符合条件,所以最终的结果也符合条件;
dp[ i ][ j ]=max( dp[ i ][ j-1 ] ,max( dp[ i-1 ][ k ] ) )+num[ i ]; ( i-1<=k<j );
 
表:
         1       2       3       4      5       6      7
0      -2      11     -4      13     -5      6      -2
1      -2     11      7      20     15    21     19
2             9       7       24     19     26     24
3                      5        22     19    30     28
4                                18      17    28     28
5                                          13    24     26
6                                                   19    22
7                                                          17
 
如表,首先是1行1列满足,再是2行,三行,四行满足,这样下去所有都满足;
 如果用dp[ i ][ j ]去存数据会占用很多内存,可能会超内存;
可以发现,在运行时只有两行是处于运行的,其他的之后没用过,因此可以用两个数组去保存这两行,然后不断更新这两行;
用pre[ ]数组去保存前一行,用dp[ ]数组去保存后一行;
int dp[maxn],pre[maxn],arr[maxn];
int temp,n,m;

arr[ ]储存输入的数;

        for(int k=;k<=m;k++)
{
temp=-inf;
for(int j=k;j<=n;j++)
{
dp[j]=max(dp[j-],pre[j-])+arr[j];
pre[j-]=temp;
temp=max(temp,dp[j]);
}
}

用temp来找出 j 个数取 k 组所得的组的最大和;同时把它记入在pre[ ]中,用于进行下次更新;

dp[j]=max(dp[j-1],pre[j-1])+arr[j];此时dp[j]和dp[j-1]有同样的组数,

dp[j]=dp[j-1]+arr[j]; // 表示把第j个数加入dp[j-1]的其中一组,能保持组数不变;

dp[j]=pre[j-1]+arr[j]; // 表示让第j个数独成一组,再加上比dp[j]少一组的组集中的最大的;

两者中选择大的组合方式;

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std; const int maxn=+;
const int inf=0x3f3f3f3f; int dp[maxn],pre[maxn],arr[maxn];
int temp,n,m; int main()
{
while(~scanf("%d%d",&m,&n))
{
for(int i=;i<=n;i++)
{
scanf("%d",&arr[i]);
}
memset(dp,,sizeof(dp));
memset(pre,,sizeof(pre));
for(int k=;k<=m;k++)
{
temp=-inf;
for(int j=k;j<=n;j++)
{
dp[j]=max(dp[j-],pre[j-])+arr[j];
pre[j-]=temp;
temp=max(temp,dp[j]);
}
}
printf("%d\n",temp);
}
return ;
}

动态规划 hdu 1024的更多相关文章

  1. 动态规划-----hdu 1024 (区间连续和)

    给定一个长度为n的区间:求m段连续子区间的和 最大值(其中m段子区间互不相交) 思路: dp[i][j]: 前j个元素i个连续区间最大值 (重要 a[j]必须在最后一个区间内) 转移方程:dp[i][ ...

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

  3. HDU 1024 max sum plus

    A - Max Sum Plus Plus Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I6 ...

  4. HDU 1024 Max Sum Plus Plus --- dp+滚动数组

    HDU 1024 题目大意:给定m和n以及n个数,求n个数的m个连续子系列的最大值,要求子序列不想交. 解题思路:<1>动态规划,定义状态dp[i][j]表示序列前j个数的i段子序列的值, ...

  5. 怒刷DP之 HDU 1024

    Max Sum Plus Plus Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u S ...

  6. Max Sum Plus Plus HDU - 1024

    Max Sum Plus Plus     HDU - 1024 Now I think you have got an AC in Ignatius.L's "Max Sum" ...

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

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

  9. hdu 1024(滚动数组+动态规划)

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

随机推荐

  1. js的事件循环机制:同步与异步任务(setTimeout,setInterval)宏任务,微任务(Promise,process.nextTick)

    javascript是单线程,一切javascript版的"多线程"都是用单线程模拟出来的,通过事件循环(event loop)实现的异步. javascript事件循环 事件循环 ...

  2. C# - Visual Studio简明操作

    Visual Studio简明操作 安装Northwind示例数据库 运行安装程序,结束安装后,再CMD中输入以下命令 cd C:\SQL Server  Sample Databases(回车) s ...

  3. Pycharm工具导入requests包(python新手)

    在学习使用python的过程中选择了工具Pycharm,但是如下代码: ,起初导包一直报错,解决办法:File->Setting 点击右上角+号,打开搜素对话框 搜素需要的导包,并加入即可解决此 ...

  4. Python爬虫实战一之爬取QQ音乐

    一.前言   前段时间尝试爬取了网易云音乐的歌曲,这次打算爬取QQ音乐的歌曲信息.网易云音乐歌曲列表是通过iframe展示的,可以借助Selenium获取到iframe的页面元素, 而QQ音乐采用的是 ...

  5. SpringBoot图片上传(五) 上一篇的新版本,样式修改后的

    简单描述:一次上传N张图片(N可自定义):上传完后图片回显,鼠标放到已经上传的图片上后,显示删除,点击后可以删除图片,鼠标离开后,图片恢复. 效果:一次上传多个图片后的效果 上传成功: 鼠标悬浮到图片 ...

  6. 经测试,foreach循环比linq的效率高

  7. python 的bif

    函数在类里面就叫做方法from sys import pi 导入sys模块里面的pi方法import sys as system 命名为新的名字dir(sys) 查看sys模块里面都提供了哪些东西he ...

  8. Linux--奇思淫才

    根据进程号找到可执行的文件路径 [ec2-user@baolin ~]$ ll /proc/<pid>/exe lrwxrwxrwx 1 ec2-user ec2-user 0 May 3 ...

  9. 期货大赛项目|八,ueditor的应用

    百度开发的富文本编辑器还是很不错的,可以已经不维护了 下载ueditor1_4_3_3-utf8-net放到项目中 找到net文件夹下config.json 修改以下两行 "imageUrl ...

  10. excel数据导入mysql

    先把excel数据另存成txt文件 Load Data InFile 'D:/1.txt' Into Table `res_type_collect` fields terminated by '@‘ ...