kuangbin求带飞DP1 Max Sum Plus Plus(动态规划+滚动数组)
来源:互联网 发布:淘宝达人直播怎么赚钱 编辑:程序博客网 时间:2024/05/22 05:03
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(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. ^_^
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. ^_^
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.
Process to the end of file.
Output
Output the maximal summation described above in one line.
Sample Input
1 3 1 2 32 6 -1 4 -2 3 -2 3
Sample Output
68
总结:
这道题很有很有意思,一开始拿到这道题的时候基本上完全没有思路,主要是抓不准状态的选取,看了题解以后也不是很懂,但其实仔细思考后发现这道题的思路和他的低配版Max Sum其实是差不多的,这才发现其实Max Sum的思想还是没有搞得太清楚,先从maxsum说起,maxsum想要找到最大的连续子串,解决的方式很dp,并不急于寻找这个子序列,而是每个让每个元素都去寻找使自己最大的最长子序列,那么当然只有两种决策,一种是加入前方的序列,另一种是自己重新开始一段序列,这种方式又恰好能够使后面的元素来判断是否加入前面序列,真是太漂亮了!!
回到这道题,其实道理是一样的只是状态多了一维,和上一道题一样我们去遍历每个元素的状态,使得自己所在的连续连续子串和最大,那么相同道理,对于一个元素还是只有两个选择,一个是加入到前面的串中,另一个是自己重新开始一个串,决策就是这两个,但是由于我们定义的状态和刚刚有所不同,所以我们状态转移的方式有所不同了。
f[j][m]表示在第j位,m段子串的和的最大值,那么就有
f[j][m]=max{ f[j-1][m]+num[j], f[k][m-1] (k=m-1...j-1) } 还是很好理解的
//////// main.cpp//// Max Sum Plus Plus//////// Created by 张嘉韬 on 16/8/12.//// Copyright © 2016年 张嘉韬. All rights reserved.////////#include <iostream>//#include <cstring>//#include <cstdio>//using namespace std;//const int maxn=10000+10;//int a[maxn],sum[maxn][maxn],f[maxn][maxn];//int main(int argc, const char * argv[]) {// freopen("/Users/zhangjiatao/Documents/暑期训练/input.txt","r",stdin);// int m;// while(scanf("%d",&m)!=EOF)// {// int n;// scanf("%d",&n);// for(int i=1;i<=n;i++)// {// scanf("%d",&a[i]);// sum[i][i]=a[i];// }// for(int i=1;i<=n;i++)// {// for(int j=i+1;j<=n;j++)// {// sum[i][j]=sum[i][j-1]+a[j];// }// }// for(int i=1;i<=n;i++)// {// for(int j=i;j<=n;j++)// {// cout<<sum[i][j]<<" ";// }// cout<<endl;// }// cout<<endl;// for(int i=1;i<=n;i++)// {// for(int x=1;x<=m;x++)// {// int maximum=0;// for(int j=1;j<=i-1;j++)// {// if(x-1>0)// {// int temp=f[j][x-1]+sum[j+1][i];// if(temp>maximum) maximum=temp;// }// }// f[i][x]=maximum;// }// }// printf("%d\n",f[n][m]);// }// return 0;//}#include <iostream>#include <cstring>#include <cstdio>#include <cmath>using namespace std;const int maxn=1000000+10;const int inf=0x7fffffff;int a[maxn],f[maxn],record[maxn];int main(){ //freopen("/Users/zhangjiatao/Documents/暑期训练/input.txt","r",stdin); int m; while(scanf("%d",&m)!=EOF) { int n,maximum; scanf("%d",&n); memset(f,0,sizeof(f)); memset(record,0,sizeof(record)); for(int i=1;i<=n;i++) scanf("%d",&a[i]); for(int i=1;i<=m;i++) { maximum=-inf; for(int j=i;j<=n;j++) { f[j]=max(f[j-1]+a[j],record[j-1]+a[j]); record[j-1]=maximum; maximum=max(f[j],maximum); } } printf("%d\n",maximum); }}
0 0
- kuangbin求带飞DP1 Max Sum Plus Plus(动态规划+滚动数组)
- Hdu 1024 Max Sum Plus Plus 动态规划+滚动数组
- hdu 1024 Max Sum Plus Plus (滚动数组 &&动态规划)
- Max Sum Plus Plus 动态规划
- HDU - 1024 Max Sum Plus Plus(DP + 滚动数组)
- HDU 1024 Max Sum Plus Plus(动态规划)
- HDU:1024 Max Sum Plus Plus(动态规划)
- hdu1024 Max Sum Plus Plus(动态规划)
- HDU1024 ——Max Sum Plus Plus(动态规划)
- 杭电ACM OJ 1024 Max Sum Plus Plus 动态规划 二维dp+滚动数组dp优化
- HDU 1024 Max Sum Plus Plus (DP·滚动数组)
- DP+滚动数组-HDU-1024-Max Sum Plus Plus
- HUD-1024-Max Sum Plus Plus DP+滚动数组
- HDU 1024 Max Sum Plus Plus DP+滚动数组
- HDU 1024 Max Sum Plus Plus【DP+滚动数组】
- HDOJ 1024 Max Sum Plus Plus -- 动态规划
- HDU 1024 Max Sum Plus Plus 动态规划
- HDU 1024(Max Sum Plus Plus)动态规划
- JSPath原理
- sdut oj3400 数据结构实验之排序三:bucket sort
- 一致性模型
- spark操作hbase的javaapi
- Kotlin语法(六)-类和继承
- kuangbin求带飞DP1 Max Sum Plus Plus(动态规划+滚动数组)
- Python3爬虫学习笔记1.2——模拟登录
- POJ 3214 Object Clustering 哈夫曼距离最小生成树
- poj1276 多重背包
- 题解:艾米利亚的求助
- Android入门学习——之CSS
- POJ-3666 Making the Grade
- Spring字符集过滤器CharacterEncodingFilter
- kafka_2.11-0.10.0.1遇到的问题