- 1、本文档共11页,可阅读全部内容。
- 2、有哪些信誉好的足球投注网站(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
- 3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载。
- 4、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
搬寝室
Dynamic Programming
---moving into a new dormitory
Problem
Question
Can you find the optimal solution to the lowest sense of tiring to help the poor Mary?
n is the total number of her baggage
2*k is the number of baggage she will move.
(2=2*k=n2000)
w[i] is the weight of the No.i baggage;
dp[i][j] is the lowest sense of tiring when moving 2*i pieces of baggage from j pieces of baggage.
DP Step 1: characterizing the optimal sub-structure
Moving into a new dormitory problem has the optimal sub-structure.
The optimal solution for the lowest sense of tiring is dp[k][n]
If we move the No.n baggage,the lowest sense of tiring dp[k-1][n-2] should also be optimal; or else we can replace the non-optimal solution with an optimal one
If we don’t move the No.n baggage,the lowest sense of tiring dp[k][n-1](= dp[k][n] )should also be optimal; or else we can replace this non-optimal solution with an optimal one
DP Step 2: construct the recursion solution
Case 1: We do not move No.j baggage.
– We select best 2*i pieces from { 1, 2, …, j-1 }
Case 2: We move No.j baggage.
–If we move j ,we must move j-1,as we should move a pair of baggage.
– We select best 2*(i-1) pieces from { 1, 2, …, j-2 }
Assuming k=1
when n=2,there is only one choice,that is,moving w[1] and w[2] together.So dp[1][2]=(w[1]-w[2])^2.
when n=3,there are two choices,that is,w[1],w[2]or w[2],w[3].So dp[1][3]=min{dp[1][2],dp[0][1]+(w[2]-w[3])^2}
Assuming k=2
when n=4,there is only one choice,that is ,moving w[1], w[2] together and moving w[3],w[4] together.So dp[2][4]=(w[1]-w[2])^2+(w[3]-w[4])^2.
when n=5,dp[2][5]=min{dp[2][4],dp[1][3]+(w[4]-w[5])^2}.
……
Code
input k,n, w[1],….w[n];
Make an array dp[0][0],….,dp[k][n] ;
for i=0 to n
dp[0][i]=0;
sort(w);
for i=1 to k
{
dp[i][i+i]=dp[i-1][i+i-2]+(w[i+i-1]-w[i+i])*(w[i+i-1]-w[i+i]);
for j=2*i+1 to n
dp[i][j]=min(dp[i][j-1],dp[i-1][j-2]+(w[j]-w[j-1])*(w[j]-w[j-1
文档评论(0)