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 3 2 6 -1 4 -2 3 -2 3
Sample Output
6 8
总结:
这道题很有很有意思,一开始拿到这道题的时候基本上完全没有思路,主要是抓不准状态的选取,看了题解以后也不是很懂,但其实仔细思考后发现这道题的思路和他的低配版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); } }