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

InputEach test case will begin with two integers m and n, followed by n integers S 1, S 2, S 3 ... S n.
Process to the end of file.
OutputOutput 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段子段和问题,设sum[n][m]为前n个数,m段子段和的最大值,状态转移有三:
1.a[n]不属于该第m段最大和,sum[n][m] = sum[n-1][m]
2.a[n]属于该第m段最大和,且为新的一段 sum[n][m] = sum[n-1][m-1] + a[n]
3.a[n]属于该第m段最大和,且不为新的一段 sum[n][m] = sum[n-1][m] + a[n]
由状态转移方程易知,1与3互相矛盾,就要寻找第二个状态转移方程来辅助计算,令b[n][m]为包含a[n]的前n个数的m段最大子段和,则sum[n][m]为前n个数的m段最大子段和,a[n]不一定包含在内,据此知,sum[n][m] = max(b[j][m])(m<=j<=n),最终答案就是sum[n][m]
由上述分析,可得出b[n][m]的状态转移方程,a[n]属于第m段/不属于第m段,即b[n][m] = max(b[n-1][m], sum[n-1][m-1]) + a[n],通过该式可以得出计算顺序,先计算b[n][m],其中需要sum[n-1][m-1]与b[n-1][m],再更新sum[n][m]),而在计算sum[n][m] = max(b[j][m])(n<=j<=m),类比完全背包的优化,在计算sum[n][m]时,sum[n][m] = max(b[j][m])(m<=j<=n) = max(b[n][m], sum[n-1][m]),因为sum[n-1][m] = max(b[x][m])(m<=x<=n-1),等式成立,我们枚举的顺序是从小到大,所以在sum[n][m]计算之前sum[n-1][m]已经计算好了,不需要再重复枚举了
const int maxm = ;

int sum[maxm][maxm], b[maxm][maxm], a[maxm];

int main() {
ios::sync_with_stdio(false), cin.tie();
int n, m;
int tmp;
while(cin >> m >> n) {
memset(sum, , sizeof(sum)), memset(a, , sizeof(a)), memset(b, , sizeof(b));
for(int i = ; i <= n; ++i) cin >> a[i];
for(int i = ; i <= m; ++i) {
for(int j = i; j <= n; ++j) {
b[j][i] = max(b[j-][i], sum[j-][i-]) + a[j];
sum[j][i] = max(sum[j-][i], b[j][i]);
}
}
cout << sum[n][m] << endl;
}
return ;
}
但是注意,该题的范围是1e6,二维显然空间炸了,就需要优化为一维的滚动数组形式,我们将计算的式子都列出来:
b[n][m] = max(b[n-1][m], sum[n-1][m-1]) + a[n], sum[n][m] = max(sum[n-1][m], b[n][m])
滚动时从小到大,该位置为j,则j之前的是[][m],j之后的是[][m-1](上一轮的),则先计算b[],再更新sum[],因为sum和b有冲突,先sum[n-1][m-1],再sum[n-1][m],就用一个tmp来记录
const int maxm = 1e6+;

int sum[maxm], b[maxm], a[maxm];

int main() {
ios::sync_with_stdio(false), cin.tie();
int n, m;
int tmp;
while(cin >> m >> n) {
memset(sum, , sizeof(sum)), memset(a, , sizeof(a)), memset(b, , sizeof(b));
for(int i = ; i <= n; ++i) cin >> a[i];
for(int i = ; i <= m; ++i) {
tmp = -0x3f3f3f3f;
for(int j = i; j <= n; ++j) {
b[j] = max(b[j-], sum[j-]) + a[j]; // 此处sum[j-1] 是第i-1段
sum[j-] = tmp; // 更新sum[j-1] 为第i段的, 该tmp是第i段的j-1,由上一次的循环继承而来
tmp = max(sum[j-], b[j]); // 更新tmp, 该tmp是第i段的j,为下一次循环中的的j-1
}
sum[n] = tmp;
}
cout << sum[n] << "\n";
}
return ;
}

参考微博:https://www.cnblogs.com/chuckcpc/p/dp_Max_M_Sum.html

最大m段子段和 Day9 - E - Max Sum Plus Plus HDU - 1024的更多相关文章

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

  2. C - Max Sum Plus Plus HDU - 1024

    用二位数组dp[i][j]记录组数为i,前j个数字的最大子段和. 转移方程: dp[i][j],考虑第j个数,第j个数可以并到前面那一组,此时dp[i][j]=dp[i][j-1]+arr[j],第j ...

  3. Max Sum Plus Plus HDU - 1024 基础dp 二维变一维的过程,有点难想

    /* dp[i][j]=max(dp[i][j-1]+a[j],max(dp[i-1][k])+a[j]) (0<k<j) dp[i][j-1]+a[j]表示的是前j-1分成i组,第j个必 ...

  4. 洛谷P1121 环状最大两段子段和

    题目描述 给出一段环状序列,即认为A[1]和A[N]是相邻的,选出其中连续不重叠且非空的两段使得这两段和最大. 输入输出格式 输入格式: 输入文件maxsum2.in的第一行是一个正整数N,表示了序列 ...

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

  6. 洛谷 P1121 环状最大两段子段和 解题报告

    P1121 环状最大两段子段和 题目描述 给出一段环状序列,即认为\(A_1\)和\(A_N\)是相邻的,选出其中连续不重叠且非空的两段使得这两段和最大. 输入输出格式 输入格式: 第一行是一个正整数 ...

  7. (最大m子段和) Max Sum Plus Plus (Hdu 1024)

    http://acm.hdu.edu.cn/showproblem.php?pid=1024     Max Sum Plus Plus Time Limit: 2000/1000 MS (Java/ ...

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

  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. python3练习100题——029

    原题链接:http://www.runoob.com/python/python-exercise-example29.html 题目:给一个不多于5位的正整数,要求:一.求它是几位数,二.逆序打印出 ...

  2. 3ds Max File Format (Part 2: The first inner structures; DllDirectory, ClassDirectory3)

    Now that we understand the outer structure of the file, it's time to look closer to what's inside. T ...

  3. sysbench下载与安装

    目标:下载.安装sysbench软件,做数据库压测 准备: 在sysbench启动的linux机器上,首先安装好mysql,查看mysql已经启动 例如,在机器上已经安装完mysql,其路径为 /us ...

  4. [SHOI2016] 黑暗前的幻想乡 - 矩阵树定理,容斥

    #include <bits/stdc++.h> using namespace std; #define int long long const int N = 20; const in ...

  5. [CCPC2019 哈尔滨] L. LRU Algorithm - 哈希

    [CCPC2019 哈尔滨] L. LRU Algorithm Description 对一个序列执行 LRU 算法.每次询问给定一个窗口,问它是否出现过. Solution 很显然我们可以先假设窗口 ...

  6. [UOJ228] 基础数据结构练习题 - 线段树

    考虑到一个数开根号 \(loglog\) 次后就会变成1,设某个Node的势能为 \(loglog(maxv-minv)\) ,那么一次根号操作会使得势能下降 \(1\) ,一次加操作最多增加 \(l ...

  7. Learn from Niu 2020.1.13

    观念: 1. 把可视化的东西拾起来, 毕竟是做那个出身的. 2. 可视化里面时序数据,时空数据一直都是非常重要的. 3. know your data永远是最重要的一步, 我想更好的方式是,数据驱动, ...

  8. matplotlib 画封闭图像并填充

    1.画矩形 这个费了我半天劲,不知怎么就可以了. 复制来自:https://www.cnblogs.com/ymjyqsx/p/7390288.html import  matplotlib.pypl ...

  9. jdk动态代理底层实现

    一.代理设计模式 代理设计模式是Java常用的设计模式之一. 特点: 01.委托类和代理类有共同的接口或者父类: 02.代理类负责为委托类处理消息,并将消息转发给委托类: 03.委托类和代理类对象通常 ...

  10. DVWA全级别之File Inclusion(文件包含)

    File Inclusion File Inclusion,意思是文件包含(漏洞),是指当服务器开启allow_url_include选项时,就可以通过php的某些特性函数(include(),req ...