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
 
题意:给你一个序列n个数组成,然后让你在里面找到m个字子串(不能有交叉,也不能有连接 的情况),让这m个子串的和最大。
题解:一眼扫过去就是DP,划分问题,第j个选择或者不选, 选(是继续上一个子串还是重新开一个子串),不选(不考虑,所以需要维护一个当前的最大值(ans/tmax))
   最初的状态转移方程:d[i][j]=max(d[i][j-1],d[i-1][k])+num[j],其中k=i-1,i,...,j-1;(没有优化的话大概三重循环)
   数据给的很大,有两个方面的优化:时间和空间,
   优化:时间(选择k的那一重循环可以用空间换,用pre[i]来表示到i的时候(不包括num[i])的最大值),循环的时候更新一下,就可以不用循环k那一层了;
      空间:二维数组优化为一维数组,d[i][j]=max(d[i][j-1], pre[j-1])+num[j], 写出pre后明显可以直接去掉一个维度;
      优化后的状态转移方程式:d[j]=max(d[j-1],pre[j-1])+num[j]
反思:这题写的时候,初始方程可以正常的写出来的, 但是用个pre数组来直接去掉k的那一层循环是不会的,思维有点狭隘,一直就想着通过类似01背包的方法来优化, 而没有想到再开一个数组就解决了。
 
  1. #include <cstdio>
  2. #include <iostream>
  3. #include <cstring>
  4. using namespace std;
  5.  
  6. const int INF=0x3f3f3f3f;
  7. const int maxn=1e6+;
  8. int f[maxn], pre[maxn], a[maxn];
  9.  
  10. int main()
  11. {
  12. //freopen("in.txt", "r", stdin);
  13. int m,n;
  14. while(cin>>m>>n)
  15. {
  16. for(int i=; i<=n; i++)
  17. cin>>a[i];
  18.  
  19. memset(f, , sizeof(f));
  20. memset(pre, , sizeof(pre));
  21.  
  22. int tmax;
  23. for(int i=; i<=m; i++)
  24. {
  25. tmax=-INF;
  26. for(int j=i; j<=n; j++)
  27. {
  28. f[j]=max(f[j-], pre[j-])+a[j];
  29. pre[j-]=tmax; //注意pre的更新的顺序,pre[j-1]被使用后再更新pre[j-1]
  30. tmax=max(tmax, f[j]);
  31. }
  32. }
  33. cout<<tmax<<endl;
  34. }
  35. return ;
  36. }
  1.  

HDU 1024 Max Sum Plus Plus(DP的简单优化)的更多相关文章

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

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

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

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

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

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

  6. HDU 1024 Max Sum Plus Plus(基础dp)

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

  7. HDU 1024 max sum plus

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

  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

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

随机推荐

  1. C#路径中获取文件全路径、目录、扩展名、文件名称

    C#路径中获取文件全路径.目录.扩展名.文件名称常用函数 需要引用System.IO 直接可以调用Path的静态方法 class Program { static void Main(string[] ...

  2. Scrapy详解

    一.爬虫生态框架 在管道传数据只能传字典和items类型. 将 上一return语句注释则会报错  如: 如上图,爬虫文件中有一个name属性,如果多个爬虫可以通过这个属性在管道控制分析的是哪个爬虫的 ...

  3. js点击出现二级菜单,点击二级菜单主菜单换成二级菜单

    点击出现二级菜单 *{ margin:0px auto; padding:0px; } .yiji{ width:200px; height:40px; background-color:red; c ...

  4. 灵雀云受邀加入VMware 创新网络,共同助力企业数字化进程

        11月15日,在VMware主办的“VMware创新网络”2018高峰论坛上,VMware发布了VMware创新网络(VMwareInnovation Network,VIN)的长期发展规划和 ...

  5. 记录心得-IntelliJ iDea 创建一个maven管理的的javaweb项目

    熟能生巧,还是记录一下吧~ 开始! 第一步:File--New--Project--Maven--Create from archetype--maven-archetype-webapp 第二步:解 ...

  6. 【SW4STM32生成 hex文件的设置方法】

    SW4STM32生成 hex文件的设置方法 开发环境:WIN7_64 + SW4STM32  联系方式:yexiaopeng1992@126.com 修改: 2018年1月21日 在这周,有一个热心的 ...

  7. C语言 全局变量、静态全局变量、局部变量、静态局部变量

    //test.c #include <stdio.h> extern int global_var; void test_global_var() { global_var++; prin ...

  8. rabbitMQ Management http://localhost:15672/ 打不开

    C:\RabbitMQ Server\rabbitmq_server-3.7.7\sbin>rabbitmq-plugins enable rabbitmq_management 安装rabbi ...

  9. 区块链名词解析:ICO、IFO、IEO和IMO,分别是什么呢?

    区块链名词解析:ICO.IFO.IEO和IMO,分别是什么呢?本部分给出了标准答案,但其相当枯燥乏味,建议快进. ICO(Initial Coin Offering),首次代币发行,指区块链项目首次向 ...

  10. 取代iframe,实现页面中引入别的页面

    <li> <a href="#addChannel">添加通道</a> </li> window.onload = rightCha ...