Max Sum Plus Plus
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 17264 Accepted Submission(s): 5683
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 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因为为菜鸟,刚刚在研究动态规划,所以对状态转移方程还不熟练,所以参考了以下两位的博文: http://blog.csdn.net/lishuhuakai/article/details/8067474http://blog.csdn.net/tmljs1988/article/details/6644580 在做题过程中,将滚动数组的概念加入dp数组当中,以节省空间。关于状态转移方程: 假设dyx[k]这个数组是表示,在前k的数中分为i段的最大不相交的子序列的和,且必须包含第k个数。(dyx是我女神~)而dp[t][k]这个数组则表示,在前k个数中分为i段的最大不相交子序列的和,不一定要包含第k个数。 这个dp数组中的t是用来建立滚动数组的.我们如果想要知道dyx[i][k]的值,因为我们在这一定要包含第k个数,那么分为以下两种情况:1. 第k个数单独为1段-->dyx[i][k-1]+a[k]; 2. 第k个数与前面的数连在一起-->dp[i-1][k-1]+a[k];则dyx[i][k]=max(dyx[i][k-1],dp[i-1][k-1]);而dp[i][k]的值,也分为两种情况;1.包含第k个数。 2. 不包含第k个数。dp[i][k]=max(dp[i][k-1],dyx[i][k]);在滚动数组中,我们只需要知道当前所分的段数,和前一分段数的值即可。dp[0]分为0段,dp[1]分为1段,dp[0]分为2段,dp[1]分为3段。最后输出只要知道它循环几次即可。
</pre><pre name="code" class="cpp">
</pre><pre name="code" class="cpp"> #include <iostream> using namespace std; const long Max=1000001; //开一个二维的dp数组,注意要建立滚动数组。 int dp[2][Max]; int sum[Max];//定义一个求前n项和的数组。 int dyx[Max]; int cmax(int a,int b) { return a>b?a:b; } int main() { //此题的关键在于,在求取前k个数分成i段时,到底是否要将第k个数加上。 //在这运用了滚动数组的想法,用于节省空间开销。 int m,n;//m为输入的数据将要分成m段,而n为将会输入n个数据。 int i;//表示在现阶段要进行截取i段。 int k;//将要输入的数据。 while(cin>>m>>n) { sum[0]=0;//赋值和输入数据 for(i=1;i<=n;i++) { cin>>k; sum[i]=sum[i-1]+k;//求取前k个数的和。 //先将dp的数组的0值找出、 dp[0][i]=0;//将前k个数分成0段显然是无意义的。 } int t=1;//运用于滚动数组。 for(i=1;i<=m;i++)//分成i段 { for(k=i;k<=n;k++)//是k从i开始,因为k<i无意义,i表示分的段数。 { //用到动态规划的思想了。 //先考虑将k的数分为k段的情况,如下。 if(i==k) { dp[t][k]=dyx[k]=sum[k];//将k个数分成k段,即为前k个数的和。 } else { dyx[k]=cmax(dp[1-t][k-1],dyx[k-1])+sum[k]-sum[k-1]; dp[t][k]=cmax(dp[t][k-1],dyx[k]); } } t=1-t;//使t不断地在0与1之间交替---滚动数组。 } cout<<dp[m%2][n]<<endl;//看循环了几次 } return 0; }</span><span style="font-size:24px;"> </span>