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

Max Sum Plus Plus

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 ≤ jxor 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段子序列,求最大可能达到的总和。
 #include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;
#define N 1000005
#define INF -1000000000
int num[N],dp[N],mmax[N];
/*
状态DP[i][j]
前j个数可以分成i组的和的最大值
DP[i][j]=max(dp[i][j-1]+num[j], max( dp[i-1][k] )+num[j]),0<k<j;
dp[i][j-1]是把num[j]加入到前面的一个组,
max(dp[i-1][k])+num[j]是把num[j]重新分在另一个组里面,
而max(dp[i-1][k])是分成i-1个组时候的和的最大值
开一个mmax数组每次都可以保存分成i-1个组的时候使用前j-1个数时候最大值
时间复杂度O(mn)
*/
int main()
{
int n,m;
while(~scanf("%d%d",&m,&n)){
for(int i=;i<=n;i++){
scanf("%d",num+i);
}
memset(dp,,sizeof(dp));
memset(mmax,,sizeof(mmax));
int ans;
for(int i=;i<=m;i++){
ans=INF;
for(int j=i;j<=n;j++){
dp[j]=max(dp[j-],mmax[j-])+num[j];
mmax[j-]=ans;
ans=max(dp[j],ans);
}
}
cout<<ans<<endl;
}
return ;
}

2016-06-21

HDU 1024:Max Sum Plus Plus(DP)的更多相关文章

  1. HDU 1024:Max Sum Plus Plus(DP,最大m子段和)

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

  2. HDU 1024 Max Sum Plus Plus(dp)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1024 题目大意:有多组输入,每组一行整数,开头两个数字m,n,接着有n个数字.要求在这n个数字上,m块 ...

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

  4. hdoj Max Sum Plus Plus(DP)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1024 题意:----最大M子段和问题给定由 n个整数(可能为负整数)组成的序列a1,a2,a3,……, ...

  5. HDU1024 Max Sum Plus Plus(dp)

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=1024 #include<iostream> #include<vector> #i ...

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

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

  8. HDU 1003:Max Sum(DP,连续子段和)

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

  9. HDU 1003:Max Sum

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

随机推荐

  1. for循环计数

    1.巧用for循环计数,将文件每10行写到另一个文件,每遍历一行i就加1 with open('/etc/passwd') as f1, open('/tmp/passwd','w') as f2: ...

  2. Android中颜色的设置

    方案一:新建xml文件,然后在java中用代码访问(xml文件可以直接访问) 1.在res->values文件夹下新建color.xml(这个文件中定义的代码是#RRGGBB) 2.在java类 ...

  3. c# yyyyMMdd,dd/MM/yyyy 类型字符串转换为datetime 类型

    DateTime ConvertDate = DateTime.ParseExact("20140504", "yyyyMMdd", null, System. ...

  4. Lintcode: Minimum Subarray

    Given an array of integers, find the subarray with smallest sum. Return the sum of the subarray. Hav ...

  5. DIY小能手|别买电动滑板车了,咱做一台吧

    !! http://www.shoudian.org/thread-316111-1-1.html http://www.jiequer.com/html/news/xinpin/2014/1218/ ...

  6. ajax常用参数

    url: 要求为String类型的参数,(默认为当前页地址)发送请求的地址.前台跳转到后台 请求参数:前台向后台传数据 回调函数:回调函数就是一个自定义的函数在发生特定的事件的时候调用来处理这个事件 ...

  7. 三台CentOS 5 Linux LVS 的DR 模式http负载均衡安装步骤

    Linux负载均衡软件LVS(概念篇) 一. LVS简介 LVS是Linux Virtual Server的简称,也就是Linux虚拟服务器, 是一个由章文嵩博士发起的自由软件项目,它的官方站点是ww ...

  8. android studio adb

    bogon:platform-tools alamps$ echo $HOME /Users/alamps bogon:platform-tools alamps$ echo $PATH /usr/l ...

  9. paper 73 :HDR(High Dynamic Range Imaging)在摄影中指高动态范围成像

    HDR(High Dynamic Range Imaging)在摄影中指高动态范围成像.国内的教程基本语焉不详,找到一篇比较详尽的国外教程翻译出来,希望对大家有帮助.^_^ 原文地址:http://p ...

  10. 搞笑的u盘图片