Max Sum Plus Plus

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 21336    Accepted Submission(s): 7130

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.

具体解释见代码:
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <cstring>
#pragma warning(disable:4996)
using namespace std; #define maxn 1000002
#define minn -1*(1e9+7) int n, m;
int dp[maxn], b[maxn], val[maxn]; int main()
{
//freopen("i.txt","r",stdin);
//freopen("o.txt","w",stdout); int i, j;
int res;
while (scanf("%d%d", &m, &n) != EOF)
{
for (i = 1; i <= n; i++)
{
scanf("%d", val + i);
}
memset(dp, 0, sizeof(dp));
memset(b, 0, sizeof(b)); //dp[i][j]表示i个数分为j组且在选取了第i个数的前提下的最大值
//dp[i][j]=max(dp[i-1][j]+a[j],max(dp[0][j-1]~dp[i-1][j-1])+a[j])
//dp[x]表示第i轮的dp[x][i],即表示x个数时分成i个组的最大值
//b[x]表示上一轮所有的最大值,即第j轮时,b[x]=max(dp[0][j-1]~dp[x-1][j-1])
for (j = 1; j <= m; j++)
{
res = minn;
for (i = j; i <= n; i++)
{
//表示dp[j][i]只有两种可能来源,一个是dp[j-1][i]+val[j],一个是max(dp[0][j-1]~dp[i-1][j-1])+a[j]
dp[i] = max(dp[i - 1] + val[i], b[i - 1] + val[i]);
b[i - 1] = res;
res = max(res, dp[i]);
}
}
printf("%d\n", res);
}
//system("pause");
return 0;
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

HDU 1024:Max Sum Plus Plus 经典动态规划之最大M子段和的更多相关文章

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

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

  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【DP,最大m子段和】

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1024 题意: 给定序列,给定m,求m个子段的最大和. 分析: 设dp[i][j]为以第j个元素结尾的 ...

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

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

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

  7. HDU 1024 Max Sum Plus Plus [动态规划+m子段和的最大值]

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

  8. hdu 1024 Max Sum Plus Plus (动态规划)

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

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

随机推荐

  1. C语言中的快速排序函数

    C库中有自带的快排函数 qsort() ; 它的函数原型为: void qsort(void * , size_t ,size_t size , int (__cdecl *)(const  void ...

  2. js判断ie和edge是否安装Adobe Reader PDF阅读器

    ie浏览器和edge浏览器,必须用Adobe Reader PDF阅读器才可以打开pdf文件,其他现代浏览器自带pdf阅读器,无需安装. 判断ie或者edge如果安装了,就浏览pdf文件:如果没安装就 ...

  3. jquery移除click事件

    原文链接:https://blog.csdn.net/weixin_41228949/article/details/83142661 在html中定义click事件有两种方式,针对这两种方式有两种移 ...

  4. PAT A1103 Integer Factorization

    线性dfs,注意每次深搜完状态的维护~ #include<bits/stdc++.h> using namespace std; ; vector<int> v,tmp,pat ...

  5. 吴裕雄--天生自然ORACLE数据库学习笔记:Oracle数据备份与恢复

    run{ allocate channel ch_1 device type disk format = 'd:\oraclebf\%u_%c.bak'; backup tablespace syst ...

  6. Python学习笔记009

    不换行 print("Hello,world!",end='')print("Hello,world!",end='')print("Hello,wo ...

  7. IP show

    1. 查看本机公网IP 1.1 curl ifconfig.me 1.2 ipinfo.io 1.3 test-ipv6.com 1.4 more 2. 查看本机IP,host 2.1 hostnam ...

  8. IDEA导入项目后,导入artifacts 方法 以及 Spring的配置文件找不到的解决方法

    我们一般选择 open 项目,如果没有artifacts 的添加选项,我们就要选择 import 项目. 如果没有artifacts ,项目下面会有错误提示,点击错误提示Fix,设置里面导入artif ...

  9. elasticsearch 自定义routing

    由于线上elasticsearch集群数据量越来越大,优化已经已经是重中之重. 优化的方式有很多中,网上一大堆,自行百度. 优化方案中有个叫routing的方案是个需要熟悉业务日志才能使用.于是我就研 ...

  10. Python学习第八课——函数

    python函数(def) def test(x): # x为形参 y = x + 20 return y # def:定义函数的关键字 # test:函数名 # ():内定义参数 # x+=1:代码 ...