HDU 1024 Max Sum Plus Plus(DP)
2016-03-26 17:26
387 查看
题意:给你一串序列,问你把这个序列分成m组的最大连续和为多少
思路:最大连续和的升级版,我们令dp[i][j]为前j个数组成i个组的最大连续和,显然有两种决策,第j个数要么包含在i组中,要么另开一组,那么转移方程即为dp[i][j]=max(dp[i][j-1]+a[j],max(dp[i-1][k])+a[j]); (0<k<j),因为这里n多达一百万并且留意到第一维的转移其实只有需要一个dp[i-1][k],那么我们直接另开一个数组来表示即可。
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 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(im, 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. ^_^
Input
Each 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.
Output
Output the maximal summation described above in one line.
Sample Input
Sample Output
Hint
思路:最大连续和的升级版,我们令dp[i][j]为前j个数组成i个组的最大连续和,显然有两种决策,第j个数要么包含在i组中,要么另开一组,那么转移方程即为dp[i][j]=max(dp[i][j-1]+a[j],max(dp[i-1][k])+a[j]); (0<k<j),因为这里n多达一百万并且留意到第一维的转移其实只有需要一个dp[i-1][k],那么我们直接另开一个数组来表示即可。
#include <cstdio> #include <queue> #include <cstring> #include <iostream> #include <cstdlib> #include <algorithm> #include <vector> #include <map> #include <string> #include <set> #include <ctime> #include <cmath> #include <cctype> using namespace std; #define maxn 1000005 #define LL long long const int inf = 0x3f3f3f3f; int cas=1,T; int dp[maxn]; int pre[maxn]; int a[maxn]; int main() { int n,m; while (scanf("%d",&m)!=EOF) { scanf("%d",&n); for (int i = 1;i<=n;i++) { scanf("%d",&a[i]); dp[i]=0; pre[i]=0; } dp[0]=0; pre[0]=0; int maxx=-inf; for (int i = 1;i<=m;i++) { maxx=-inf; for (int j=i;j<=n;j++) { dp[j]=max(dp[j-1]+a[j],pre[j-1]+a[j]); pre[j-1]=maxx; maxx=max(maxx,dp[j]); } } printf("%d\n",maxx); } //freopen("in","r",stdin); //scanf("%d",&T); //printf("time=%.3lf",(double)clock()/CLOCKS_PER_SEC); return 0; }
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 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(im, 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. ^_^
Input
Each 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.
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.
相关文章推荐
- Hibernate 关联映射 之 多对多 关联(一)
- hibernate多对多,转换为两个一对多
- 软件工程个人作业03
- 第四周进度条
- codeforces 451 E Devu and Flowers
- java如何操作字符串取得绝对路径中的文件名及文件夹名
- C# 随机数组游戏
- 白盒测试-百度出来的感觉说的还是蛮详细的
- 二叉树算法总结
- Autodesk Vred 2016
- py-faster-rcnn + ZF 实现自己的数据训练与检测(二)
- OpenCV学习- 配置
- 获取手机总内存以及格式转化
- BBC: 寨卡病毒威胁美国,国际空间站宇航员返回地球
- 编写Python代码——爬取百度百科Python词条相关1000个页面数据【未完慕课】
- PHP应用日期与时间
- 单机ZooKeeper配置
- 四则运算设计03
- 返回一个整型数组中最大子数组的和
- 浏览器复制插件zeroclipboard使用指南