动态规划--HDU--1024
来源:互联网 发布:软件测试自动化书籍 编辑:程序博客网 时间:2024/06/07 13:10
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 S1, S2, S3, S4 ... Sx, ... Sn (1 ≤ x ≤ n ≤ 1,000,000, -32768 ≤ Sx ≤ 32767). We define a function sum(i, j) = Si + ... + Sj (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(i1, j1) + sum(i2, j2) + sum(i3, j3) + ... + sum(im, jm) maximal (ix ≤ iy ≤ jx or ix ≤ jy ≤ jx 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(ix, jx)(1 ≤ x ≤ m) instead. ^_^
Given a consecutive number sequence S1, S2, S3, S4 ... Sx, ... Sn (1 ≤ x ≤ n ≤ 1,000,000, -32768 ≤ Sx ≤ 32767). We define a function sum(i, j) = Si + ... + Sj (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(i1, j1) + sum(i2, j2) + sum(i3, j3) + ... + sum(im, jm) maximal (ix ≤ iy ≤ jx or ix ≤ jy ≤ jx 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(ix, jx)(1 ≤ x ≤ m) instead. ^_^
Input
Each test case will begin with two integers m and n, followed by n integers S1, S2, S3 ... Sn.
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因为为菜鸟,刚刚在研究动态规划,所以对状态转移方程还不熟练,所以参考了以下两位的博文: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>
0 0
- 动态规划--HDU--1024
- HDU 1024动态规划问题
- 动态规划-hdu 2955
- hdu 动态规划
- HDU动态规划专题
- HDU 1028 动态规划
- hdu 1078 动态规划
- HDU 2069 动态规划
- HDU 1160 动态规划
- HDU 1158 动态规划
- hdu 1176 动态规划
- hdu 1423 动态规划
- 动态规划 --- hdu 1003 **
- 动态规划 --- hdu 1176 **
- HDU 1421 动态规划
- hdu 3199 动态规划
- HDU(1003)动态规划
- hdu动态规划集锦
- jquery判断元素是否隐藏
- StringUtils常用方法+StringUtils详细介绍
- 第11周项目1函数版星号图(1)
- 第11周项目1(2)-函数版星号图(printchs)
- 编程:函数版星号图
- 动态规划--HDU--1024
- 第十一周项目1-函数版星号图2
- 第11周项目2-求最大公约数(1)
- Adapter 适配器模式
- 第11周项目1(2)-三角形星号图
- asp.net-母版页实用测试
- 5个简单方式使用strace场景
- pomelo 相关
- 第十一周项目6-回文&素数(二) .