http://acm.hdu.edu.cn/showproblem.php?pid=1024

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 S 1, S 2, S 3, S 4 ... S x, ... S n (1 ≤ x ≤ n ≤ 1,000,000, -32768 ≤ S x ≤ 32767). We define a function sum(i, j) = S i + ... + S j (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(i 1, j 1) + sum(i 2, j 2) + sum(i 3, j 3) + ... + sum(i m, j m) maximal (i x ≤ i y ≤ j x or i x ≤ j y ≤ j x 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(i x, j x)(1 ≤ x ≤ m) instead. ^_^ 

题意就是求你n个数字m段和的最大值

用动态规划求dp[n][m] 表示用前m个数划分为n块的最大和

转移方程为 dp[n][m]=max(dp[n][m-1]+a[m],max(dp[n-1][t])+a[m]),其中dp[n][m-1]+a[m]表示第m个在前i块中 后面表示的是在自己独立变成一个块(1<=t<=m-1)

那么问题来了如果 用这种方法 复杂度为(n^3)因此我们要化简一下,求max(dp[n-1][t])的时候重复求了一些过程,因此只需要改成  设一个变量M 表示前1到m-1的最大值 M=max(M,dp[i-1][j-1]);

但是空间上不够,因此用一个滚动数组 dp[2][MAN];在求M的用到了i-1;

完整代码:

#include <stdlib.h>
#include<iostream>
#include<algorithm>
//#include<bits/stdc++.h>
#include<cstdio>
#include<cstring>
#include<cmath>
#define ll long long
const ll INF=0x3f3f3f3f;
#define mod 1000000007
#define mem(a,b) memset(a,b,sizeof(a))
//__builtin_popcount
using namespace std;
//priority_queue
const ll MAX=1000000; int Max[MAX+10];
int dp[2][MAX+10];
int a[MAX+10];
int main()
{
int n,m;
while(cin>>m>>n)
{
for(int i=1; i<=n; i++)
scanf("%d",&a[i]);
mem(Max,0);
mem(dp,0);
int M;
for(int i=1; i<=m; i++)
{
M=-0x3f3f3f3f;
for(int j=i; j<=n; j++)
{
M=max(M,dp[(i-1)%2][j-1]);
if(i!=j)
dp[i%2][j]=max(dp[i%2][j-1]+a[j],M+a[j]);
else dp[i%2][j]=M+a[j];
} }
M=-0x3f3f3f3f;
for(int i=m; i<=n; i++)
M=max(M,dp[m%2][i]);
cout<<M<<endl;
} }

最新文章

  1. React-Native 组件开发方法
  2. xpath定位实战(1)
  3. AS 重装系统之后配置
  4. Bower 自定义组件文件夹名称
  5. 虚拟化之vmware-vsphere (web) client
  6. 使用 Spark 进行微服务的实时性能分析
  7. HDU 3488--Tour(KM or 费用流)
  8. redis 中文手册
  9. 在PHP中开启CURL扩展,使其支持curl()函数
  10. MySQL指令记录(Wampserve环境)
  11. 前端面试题之js篇
  12. DBCP|C3P0参数详解
  13. ADO.NET详解----核心对象的使用
  14. 大数据情报分析公司Palantir
  15. uptime
  16. AngularJS html5Mode与ASP.NET MVC路由
  17. GUI线程 :打字母游戏
  18. django Table doesn&#39;t exist
  19. js数组的用法以及数组根据下标(数值或字符)移除元素
  20. CF558E-A Simple Task-线段树+计数排序

热门文章

  1. STM32 CAN用队列缓冲接收的例子
  2. Linux基础命令---mysqldump数据库备份
  3. SVN终端演练(个人开发\多人开发)
  4. java-阿里邮件推送服务开发 -- 发送邮箱验证码
  5. 将图片打印到word中
  6. 【Java 8】函数式接口(二)—— 四大函数接口介绍
  7. C# 枚举的flags 标志位应用
  8. socket通道
  9. 童鞋,[HttpClient发送文件] 的技术实践请查收
  10. ciscn_2019_c_1 1