﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>C++博客-煙雨默嘫-随笔分类-背包</title><link>http://www.cppblog.com/yp0408100207/category/18777.html</link><description>没有伞的孩子，必须努力奔跑！</description><language>zh-cn</language><lastBuildDate>Thu, 08 Mar 2012 12:41:39 GMT</lastBuildDate><pubDate>Thu, 08 Mar 2012 12:41:39 GMT</pubDate><ttl>60</ttl><item><title>【转】HDU上DP问题汇总</title><link>http://www.cppblog.com/yp0408100207/archive/2012/03/08/167439.html</link><dc:creator>煙雨默嘫</dc:creator><author>煙雨默嘫</author><pubDate>Thu, 08 Mar 2012 12:38:00 GMT</pubDate><guid>http://www.cppblog.com/yp0408100207/archive/2012/03/08/167439.html</guid><wfw:comment>http://www.cppblog.com/yp0408100207/comments/167439.html</wfw:comment><comments>http://www.cppblog.com/yp0408100207/archive/2012/03/08/167439.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/yp0408100207/comments/commentRss/167439.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/yp0408100207/services/trackbacks/167439.html</trackback:ping><description><![CDATA[<div><div style="font-family: Arial; word-wrap: break-word; word-break: break-all; visibility: visible !important; zoom: 1 !important; filter: none; font-size: 12px; line-height: 20px; color: #666666; text-align: left; background-color: #ffffff; ">转载自&nbsp;<a href="http://hi.baidu.com/superkiki1989" target="blank" style="color: #634833; font-family: Verdana, Arial, Helvetica, sans-serif; text-decoration: none; ">superkiki1989</a></div><div style="font-family: Arial; word-wrap: break-word; word-break: break-all; visibility: visible !important; zoom: 1 !important; filter: none; font-size: 12px; line-height: 20px; color: #666666; text-align: left; margin-top: 5px; margin-right: 0px; margin-bottom: 8px; margin-left: 0px; background-color: #ffffff; ">最终编辑&nbsp;<a href="http://hi.baidu.com/youmingke" target="blank" style="color: #634833; font-family: Verdana, Arial, Helvetica, sans-serif; text-decoration: none; ">youmingke</a></div><table style="table-layout: fixed; font-family: Arial; background-color: #ffffff; width: 898px; "><tbody><tr><td style="word-wrap: break-word; word-break: break-all; visibility: visible !important; zoom: 1 !important; filter: none; font-size: 12px; line-height: 18px; "><div id="blog_text" style="word-wrap: break-word; word-break: break-all; visibility: visible !important; zoom: 1 !important; filter: none; overflow-x: hidden; overflow-y: hidden; position: relative !important; border-image: initial; ">http://acm.hdu.edu.cn/showproblem.<a title="PHP" href="http://www.aowe.net/c14.aspx" target="_blank" style="color: #634833; font-family: Verdana, Arial, Helvetica, sans-serif; text-decoration: none; line-height: normal; ">PHP</a>?pid=2955&nbsp;<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp; 背包[bao];第一次做的时候把概率[gai lv]当做背包[bao](放大100000倍化为整数):在此范围[fan wei]内最多能抢多少钱&nbsp;&nbsp; 最脑残的是把总的概率[gai lv]以为是抢N家银行的概率[gai lv]之和&#8230; 把状态[zhuang tai]转移方程写成了f[j]=max{f[j],f[j-q[i].v]+q[i].money}(f[j]表示在概率[gai lv]j之下能抢的大洋);<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp;<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp; 正确的方程是:f[j]=max(f[j],f[j-q[i].money]*q[i].v)&nbsp;&nbsp; 其中,f[j]表示抢j块大洋的最大的逃脱概率[gai lv],条件[tiao jian]是f[j-q[i].money]可达,也就是之前抢劫过;<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp; 始化为:f[0]=1,其余初始化[chu shi hua]为-1&nbsp;&nbsp; (抢0块大洋肯定不被抓嘛)<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp;<br style="line-height: normal; " />最大报销额 http://acm.hdu.edu.cn/showproblem.php?pid=1864&nbsp;<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp; 又一个背包[bao]问题[wen ti],对于每张发票,要么报销,要么不报销,0-1背包[bao],张数即为背包[bao];<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp; 转移方程:f[j]=max(f[j],f[j-1]+v[i]);<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp; 恶心地方:有这样的输入[shu ru]数据[shu ju] 3 A:100 A:200 A:300<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp;<br style="line-height: normal; " />最大连续[lian xu]子序列 http://acm.hdu.edu.cn/showproblem.php?pid=1231<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp; 状态[zhuang tai]方程:sum[i]=max(sum[i-1]+a[i],a[i]);最后从头到尾扫一边<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp; 也可以写成:<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Max=a[0];<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Current=0;<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for(i=0;i&lt;n;i++)<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if(Current&lt;0)<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Current=a[i];<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Current+=a[i];<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if(Current&gt;Max)<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Max=Current;<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp;<br style="line-height: normal; " />max sum http://acm.hdu.edu.cn/showproblem.php?pid=1003&nbsp;<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp; 同上,最大连续[lian xu]子序列&nbsp;&nbsp;&nbsp;&nbsp;<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp;<br style="line-height: normal; " />Largest Rectangle http://acm.hdu.edu.cn/showproblem.php?pid=1506<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp; 对于每一块木板,Area=height[i]*(j-k+1)&nbsp;&nbsp; 其中,j&lt;=x&lt;=k,height[x]&gt;=height[i];找j,k成为关键,一般方法[fang fa]肯定超时[chao shi],利用动态[dong tai]规划,如果它左边高度大于等于它本身,那么它左边的左边界[bian jie]一定满足这个性质,再从这个边界[bian jie]的左边迭代[die dai]下去<img src="http://www.cppblog.com/Images/dot.gif" style="line-height: normal; " alt="" /><br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp; for(i=1;i&lt;=n;i++)<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; while(a[l[i]-1]&gt;=a[i])<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; l[i]=l[l[i]-1];<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp;<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp; for(i=n;i&gt;=1;i--)<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; while(a[r[i]+1]&gt;=a[i])<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; r[i]=r[r[i]+1];<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp;<br style="line-height: normal; " />City Game http://acm.hdu.edu.cn/showproblem.php?pid=1505<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp; 1506的加强版,把2维转换[zhuan huan]化成以每一行底,组成的最大面积;(注意处理连续[lian xu]与间断的情况[qing kuang]);<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp;<br style="line-height: normal; " />Bone Collector http://acm.hdu.edu.cn/showproblem.php?pid=2602&nbsp;<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp; 简单0-1背包[bao],状态[zhuang tai]方程:f[j]=max(f[j],f[j-v[i]]+w[i])<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp;<br style="line-height: normal; " />Super Jumping&nbsp;&nbsp; http://acm.hdu.edu.cn/showproblem.php?pid=1087&nbsp;<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp; 最大递增子段[zi duan]和,状态[zhuang tai]方程:sum[j]=max{sum[i]}+a[j]; 其中,0&lt;=i&lt;=j,a[i]&lt;a[j]&nbsp;&nbsp;&nbsp;&nbsp;<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp;<br style="line-height: normal; " />命运http://acm.hdu.edu.cn/showproblem.php?pid=2571<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp; 状态[zhuang tai]方程:sum[i][j]=max{sum[i-1][j],sum[i][k]}+v[i][j];其中1&lt;=k&lt;=j-1,且k是j的因子&nbsp;&nbsp;&nbsp;&nbsp;<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp;<br style="line-height: normal; " />Monkey And Banana&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; http://acm.hdu.edu.cn/showproblem.php?pid=1069<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp; 状态[zhuang tai]方程:f[j]=max{f[i]}+v[j];其中,0&lt;=i&lt;=j,w[i]&lt;w[j],h[i]&lt;h[j]&nbsp;&nbsp;&nbsp;&nbsp;<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp;<br style="line-height: normal; " />Big Event in HDU http://acm.hdu.edu.cn/showproblem.php?pid=1171&nbsp;<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp; 一维背包[bao],逐个考虑每个物品带来的影响,对于第i个物品:if(f[j-v[i]]==0) f[j]=0;<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp; 其中,j为逆序循环[xun huan],且j&gt;=v[i]&nbsp;&nbsp;&nbsp;&nbsp;<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp;<br style="line-height: normal; " />数塔http://acm.hdu.edu.cn/showproblem.php?pid=2084<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp; 自底向上[zi di xiang shang]:dp[i][j]=max(dp[i+1][j],dp[i+1][j+1])+v[i][j];&nbsp;&nbsp;&nbsp;&nbsp;<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp;<br style="line-height: normal; " />免费馅饼http://acm.hdu.edu.cn/showproblem.php?pid=1176<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp; 简单数塔<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp; 自底向上[zi di xiang shang]计算:dp[i][j]=max(dp[i+1][j-1],dp[i+1][j],dp[i+1][j+1])+v[i][j];处理边界[bian jie]<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp;<br style="line-height: normal; " />I Need A Offer http://acm.hdu.edu.cn/showproblem.php?pid=1203<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp; 简单0-1背包[bao],题目要求的是至少收到一份Offer的最大概率[gai lv],我们得到得不到的最小概率[gai lv]即可,状态[zhuang tai]转移方程:f[j]=min(f[j],f[j-v[i]]*w[i]);其中,w[i]表示得不到的概率[gai lv],(1-f[j])为花费j元得到Offer的最大概率[gai lv]&nbsp;&nbsp;&nbsp;&nbsp;<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp;<br style="line-height: normal; " />FATE http://acm.hdu.edu.cn/showproblem.php?pid=2159&nbsp;<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp; 二维完全背包[bao],第二层跟第三层的要顺序循环[xun huan];(0-1背包[bao]逆序循环[xun huan]);状态[zhuang tai]可理解为,在背包[bao]属性[shu xing]为 {m(忍耐度), s(杀怪个数)} 里最多能得到的经验值,之前的背包[bao]牺牲体积,这个背包[bao]牺牲忍耐度跟个数<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp; 注意: 最后扫的时候 外层循环[xun huan]为忍耐度,内层循环[xun huan]为杀怪个数,因为题目要求出剩余忍耐度最大,没有约束[yue shu]杀怪个数,一旦找到经验加满的即为最优解;<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp; 状态[zhuang tai]转移方程为: f[j][k]=max(f[j][k],f[j-v[i]][k-1]+w[i]); w[i]表示杀死第i个怪所得的经验值,v[i]表示消耗的忍耐度<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp;<br style="line-height: normal; " />How To Type http://acm.hdu.edu.cn/showproblem.php?pid=2577&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp; 用两个a,b数组[shu zu]分别记录Caps Lock开与关时打印第i个字母的最少操作步骤;<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp; 而对于第i个字母的大小写还要分开讨论:<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp; Ch[i]为小写: a[i]=min(a[i-1]+1,b[i-1]+2);不开灯直接字母,开灯则先关灯再按字母,最后保持不开灯;&nbsp;&nbsp;&nbsp;&nbsp; b[i]=min(a[i-1]+2,b[i-1]+2);不开灯则先按字母再开灯,开灯则Shift+字母(比关灯,按字母再开灯节省步数),最后保持开灯;<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp; Ch[i]为大写: a[i]=min(a[i-1]+2,b[i-1]+2); b[i]=min(a[i-1]+2,b[i-1]+1)<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp;<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp; 最后,b[len-1]++,关灯嘛O(&#8745;_&#8745;)O~&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp;<br style="line-height: normal; " />Coins http://acm.hdu.edu.cn/showproblem.php?pid=2844<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp; 类似于HDU1171 Big Event In HDU,一维DP,可达可不达&nbsp;&nbsp;&nbsp;&nbsp;<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp;<br style="line-height: normal; " />Beans http://acm.hdu.edu.cn/showproblem.php?pid=2845&nbsp;<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp; 横竖分别求一下不连续[lian xu]的最大子段[zi duan]和;<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp; 状态[zhuang tai]方程: Sum[i]=max(sum[j])+a[i];其中,0&lt;=j&lt;i-1;&nbsp;&nbsp;&nbsp;&nbsp;<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp;<br style="line-height: normal; " />Largest Submatrix http://acm.hdu.edu.cn/showproblem.php?pid=2870&nbsp;<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp; 枚举[mei ju]a,b,c 最大完全子矩阵,类似于HDU1505 1506&nbsp;&nbsp;&nbsp;&nbsp;<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp;<br style="line-height: normal; " />Matrix Swapping II http://acm.hdu.edu.cn/showproblem.php?pid=2830&nbsp;<br style="line-height: normal; " />最大完全子矩阵,以第i行为底,可以构成的最大矩阵,因为该题可以任意移动列,所以只要大于等于height[i]的都可以移动到一起,求出height&gt;=height[i]的个数即可,这里用hash+滚动,先求出height[i]出现的次数,然后逆序扫一遍hash[i]+=hash[i+1];&nbsp;&nbsp;&nbsp;&nbsp;<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp;<br style="line-height: normal; " />最少拦截系统[xi tong]http://acm.hdu.edu.cn/showproblem.php?pid=1257<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp; 两种做法,一是贪心,从后往前贪;二是DP;<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp; if(v[i]&gt;max{dp[j]})&nbsp;&nbsp; (0&lt;=j&lt;len)<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp; dp[len++]=v[i];&nbsp;&nbsp;&nbsp;&nbsp;<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp;<br style="line-height: normal; " />Common Subsequence http://acm.hdu.edu.cn/showproblem.php?pid=1159&nbsp;<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp; 经典DP,最长公共子序列<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp; Len[i][j]={len[i-1][j-1]+1,(a[i]==b[j]); max(len[i-1][j],len[i][j-1])}<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp; 初始化[chu shi hua]的优化:&nbsp;<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp; for(i=0;i&lt;a;i++)<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for(j=0;j&lt;b;j++)<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; len[i][j]=0;<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for(i=1;i&lt;=a;i++)&nbsp;<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for(j=1;j&lt;=b;j++)&nbsp;<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if(ch1[i-1]==ch2[j-1])&nbsp;<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; len[i][j]=len[i-1][j-1]+1;<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else&nbsp;<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; len[i][j]=max(len[i-1][j],len[i][j-1]);&nbsp;&nbsp;&nbsp;&nbsp;<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp;<br style="line-height: normal; " />&#9733; 搬寝室http://acm.hdu.edu.cn/showproblem.php?pid=1421&nbsp;<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp; 状态[zhuang tai]Dp[i][j]为前i件物品选j对的最优解<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp; 当i=j*2时,只有一种选择[xuan ze]即 Dp[i-2][j-1]+(w[i]-w[i-1])^2<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp; 当i&gt;j*2时,Dp[i][j] = min(Dp[i-1][j],Dp[i-2][j-1]+(w[j]-w[j-1])^2)&nbsp;&nbsp;&nbsp;&nbsp;<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp;<br style="line-height: normal; " />&#9733; Humble Numbers http://acm.hdu.edu.cn/showproblem.php?pid=1058&nbsp;<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp; 如果一个数是Humble Number,那么它的2倍,3倍,5倍,7倍仍然是Humble Number<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp; 定义F[i]为第i个Humble Number<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp; F[n]=min(2*f[i],3*f[j],5*f[k],7*f[L]), i,j,k,L在被选择[xuan ze]后相互移动<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp; (通过此题理解到数组[shu zu]有序特性)&nbsp;&nbsp;&nbsp;&nbsp;<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp;<br style="line-height: normal; " />&#9733; Doing Homework Again http://acm.hdu.edu.cn/showproblem.php?pid=1789&nbsp;<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp; 这题为贪心,经典题;<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp; 切题角度,对于每个任务[ren wu]要么在截至日期前完成要么被扣分;所以考虑每个人物的完成情况[qing kuang]即可;由于每天只能完成一个任务[ren wu],所以优先考虑分值较大的任务[ren wu],看看该任务[ren wu]能不能完成,只要能完成,即使提前完成,占了其他任务[ren wu]的完成日期也没关系,因为当前任务[ren wu]的分值最大嘛,而对于能完成的任务[ren wu]能拖多久就拖多久,以便腾出更多时间完成其他任务[ren wu];&nbsp;&nbsp;&nbsp;&nbsp;<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp;<br style="line-height: normal; " />How Many Ways http://acm.hdu.edu.cn/showproblem.php?pid=1978&nbsp;<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp; 两种D法,一是对于当前的点,那些点可达;二是当前点可达那些点;<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp; 明显第二种方法[fang fa]高,因为第一种方法[fang fa]有一些没必要的尝试;<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp; Dp[i][j]+=Dp[ii][jj]; (map[ii][jj]&gt;=两点的曼哈顿距离)<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp; 值得优化的地方,每两点的曼哈顿距离可能不止求一次,所以预处理一下直接读取[du qu]&nbsp;&nbsp;&nbsp;&nbsp;<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp;<br style="line-height: normal; " />珍惜现在 感恩生活http://acm.hdu.edu.cn/showproblem.php?pid=2191&nbsp;<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp; 每个物品最多可取n件,多重[duo zhong]背包[bao];<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp; 利用二进制[er jin zhi]思想,把每种物品转化为几件物品,然后就成为了0-1背包[bao]&nbsp;&nbsp;&nbsp;&nbsp;<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp;<br style="line-height: normal; " />Piggy-Bank http://acm.hdu.edu.cn/showproblem.php?pid=1114&nbsp;<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp; 完全背包[bao];常规背包[bao]是求最大值,这题求最小值;<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp; 只需要修改[xiu gai]一下初始化[chu shi hua],f[0]=0,其他赋值[fu zhi]为+&#8734;即可;<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp; 状态[zhuang tai]转移方程:f[i][V]=max{f[i-1][V],f[i-1][V-k*v[i]]+k*w[i]},其中0&lt;=k*v[i]&lt;=V<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp;<br style="line-height: normal; " />&#9733; Max Sum Plus Plus http://acm.hdu.edu.cn/showproblem.php?pid=1024<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp; 1. 对于前n个数, 以v[n]为底取m段:&nbsp;<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp; 当n==m时,Sum[m][n]=Sum[m-1][n-1]+v[n],第n个数独立[du li]成段;<br style="line-height: normal; " />当n&gt;m时, Sum[m][n]=max{Sum[m-1][k],Sum[m][n-1]}+v[n]; 其中,m-1&lt;=k&lt;j,解释[jie shi]为,v[n]要么加在Sum[m][n-1],段数不变,要么独立[du li]成段接在前n-1个数取m-1段所能构成的最大值后面<br style="line-height: normal; " />2. 空间[kong jian]的优化:<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 通过状态[zhuang tai]方程可以看出,取m段时,只与取m-1段有关,所以用滚动数组[shu zu]来节省空间[kong jian]<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp;<br style="line-height: normal; " />FatMouse&#8217;s Speed http://acm.hdu.edu.cn/showproblem.php?pid=1160&nbsp;<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp; 要求:体重严格递增,速度严格递减,原始顺序不定<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp; 按体重或者速度排序[pai xu],即顺数固定后转化为最长上升子序列问题[wen ti]<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp; Dp[i]表示为以第i项为底构成的最长子序列,Dp[i]=max(dp[j])+1,其中0&lt;=j&lt;i , w[i]&gt;w[j]&amp;&amp;s[i]&lt;s[j] 用一个index数组[shu zu]构造最优解:记录每一项接在哪一项后面,最后用max找出最大的dp[0&#8230;n],dex记录下标[xia biao],回溯[hui su]输出[shu chu]即可&nbsp;&nbsp;&nbsp;&nbsp;<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp;<br style="line-height: normal; " />Cstructing Roads http://acm.hdu.edu.cn/showproblem.php?pid=1025&nbsp;<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp; 以p或者r按升序排列[pai lie]以后,问题[wen ti]转化为最长上升子序列<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp; 题目数据[shu ju]量比较大,只能采取二分查找[cha zhao],n*log(n)的算法[suan fa]<br style="line-height: normal; " />用一个数组[shu zu]记录dp[]记录最长的子序列,len表示长度,如果a[i]&gt;dp[len], 则接在后面,len++; 否则在dp[]中找到最大的j,满足dp[j]&lt;a[i],把a[i]接在dp[j]后面;&nbsp;&nbsp;&nbsp;&nbsp;<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp;<br style="line-height: normal; " />FatMouse Chees http://acm.hdu.edu.cn/showproblem.php?pid=1078&nbsp;<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp; Dp思想,用记忆化搜索[sou suo];简单题,处理好边界[bian jie];&nbsp;&nbsp;&nbsp;&nbsp;<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp;<br style="line-height: normal; " />To the Max http://acm.hdu.edu.cn/showproblem.php?pid=1081<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp; 最大子矩阵<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp; 把多维转化为一维的最大连续[lian xu]子序列;(HDU1003)&nbsp;&nbsp;&nbsp;&nbsp;<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp;<br style="line-height: normal; " />龟兔赛跑http://acm.hdu.edu.cn/showproblem.php?pid=2059&nbsp;<br style="line-height: normal; " />未总结&nbsp;&nbsp;&nbsp;&nbsp;<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp;<br style="line-height: normal; " />&#9733; Employment Planning http://acm.hdu.edu.cn/showproblem.php?pid=1158&nbsp;<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp; 状态[zhuang tai]表示:&nbsp;&nbsp;&nbsp;&nbsp; Dp[i][j]为前i个月的留j个人的最优解;Num[i]&lt;=j&lt;=Max{Num[i]};<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; j&gt;Max{Num[i]}之后无意义,无谓的浪费 记Max_n=Max{Num[i]};<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp; Dp[i-1]中的每一项都可能影响到Dp[i],即使Num[i-1]&lt;&lt;Num[i]<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp; 所以利用Dp[i-1]中的所有项去求Dp[i];<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp; 对于Num[i]&lt;=k&lt;=Max_n,&nbsp;&nbsp;&nbsp;&nbsp; 当k&lt;j时, 招聘;<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 当k&gt;j时, 解雇&nbsp;&nbsp; 然后求出最小值<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp; Dp[i][j]=min{Dp[i-1][k&#8230;Max_n]+(招聘,解雇,工资);&nbsp;&nbsp;&nbsp;&nbsp;<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp;<br style="line-height: normal; " />Dividing http://acm.hdu.edu.cn/showproblem.php?pid=1059&nbsp;<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp; 一维Dp&nbsp;&nbsp; Sum为偶数的时候判断Dp[sum/2]可不可达&nbsp;&nbsp;&nbsp;&nbsp;<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp;<br style="line-height: normal; " />Human Gene Factions http://acm.hdu.edu.cn/showproblem.php?pid=1080&nbsp;<br style="line-height: normal; " />状态[zhuang tai]转移方程:<br style="line-height: normal; " />f[i][j]=Max(f[i-1][j-1]+r[a[i]][b[j]], f[i][j-1]+r[&#8216;-&#8216;][b[j]],f[i-1][j]+r[a[i]][&#8216;-&#8216;]);<br style="line-height: normal; " /><br style="line-height: normal; " />&#9733; Doing Homework http://acm.hdu.edu.cn/showproblem.php?pid=1074&nbsp;<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp; 这题用到位压缩[ya suo];<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp; 那么任务[ren wu]所有的状态[zhuang tai]有2^n-1种<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp; 状态[zhuang tai]方程为:Dp[next]=min{Dp[k]+i的罚时} 其中,next=k+(1&lt;&lt;i),k要取完满足条件[tiao jian]的值 k&gt;&gt;i的奇偶[qi ou]性决定状态[zhuang tai]k<br style="line-height: normal; " />具体实现为: 对每种状态[zhuang tai]遍历[bian li]n项任务[ren wu],如果第i项没有完成,则计算出Dp[next]的最优解&nbsp;&nbsp;&nbsp;&nbsp;<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp;<br style="line-height: normal; " />Free DIY Tour http://acm.hdu.edu.cn/showproblem.php?pid=1224&nbsp;<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp; 简单的数塔Dp,考察的是细节的处理;<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp; Dp[i]=Max{Dp[j]}+v[i]&nbsp;&nbsp; 其中j-&gt;i为通路[tong lu];<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp; v[n+1]有没有初始化[chu shi hua],Dp数组[shu zu]有没有初始化[chu shi hua]<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp; 这题不能用想当然的&#8221;最长路&#8221;来解决,这好像是个NP问题[wen ti] 解决不了的<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp;<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp;<br style="line-height: normal; " />重温世界杯http://acm.hdu.edu.cn/showproblem.php?pid=1422&nbsp;<br style="line-height: normal; " />这题的状态[zhuang tai]不难理解,状态[zhuang tai]表示为,如果上一个城市剩下的钱不为负,也就是没有被赶回杭电,则再考虑它对下一个城市的影响;如果上一个城市剩下的前加上当前城市的前大于当前城市的生活费,那么Dp[i]=Dp[i-1]+1;<br style="line-height: normal; " />值得注意的而是这题的数据[shu ju]为100000;不可能以每个城市为起点来一次Dp,时间复杂度[shi jian fu za du]为n^2;足已超时[chao shi];<br style="line-height: normal; " />我是这样处理的,在保存的数据[shu ju]后面再接上1&#8230;n的数据[shu ju],这样扫描[sao miao]一遍的复杂度为n;再加一个优化,当Dp[i]==n时,也就是能全部游完所有城市的时候,直接break;<br style="line-height: normal; " /><br style="line-height: normal; " />Pearls http://acm.hdu.edu.cn/showproblem.php?pid=1300&nbsp;<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp; Dp[i]=min{Dp[j]+V},&nbsp;&nbsp; 0&lt;=j&lt;i, V为第j+1类珠宝到第i类全部以i类买入的价值;&nbsp;&nbsp;&nbsp;&nbsp;<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp;<br style="line-height: normal; " />Zipper http://acm.hdu.edu.cn/showproblem.php?pid=1501<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp; Dp[i][j]=&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp;<br style="line-height: normal; " />&#9733;Fast Food http://acm.hdu.edu.cn/showproblem.php?pid=1227<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp; 这里需要一个常识:在i到j取一点使它到区间[qu jian]每一点的距离之和最小,这一点为(i+j)/2用图形[tu xing]即可证明[zheng ming];<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp; Dp[i][j]=max{Dp[i-1][k]+cost[k+1][j]&nbsp;&nbsp; 其中,(i-1)&lt;=k&lt;j状态[zhuang tai]为前j个position建i个depots&nbsp;&nbsp;&nbsp;&nbsp;<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp;<br style="line-height: normal; " />Warcraft http://acm.hdu.edu.cn/showproblem.php?pid=3008<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp; 比赛的时候这道DP卡到我网络[wang luo]中心停电!!! 卧槽~&nbsp;<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp; 因为你没有回血效应,所以你挂掉的时间是一定的;<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp; 用Dp[i][j]表示第i秒剩余j个单位[dan wei]的MP时怪物所剩的血量; 注意必须是剩余,也就是说,初始化[chu shi hua]的时候,DP[0][100]=100;&nbsp;&nbsp; 其他Dp[0]状态[zhuang tai]都不合法,因为没有开战的时候你的MP是满的<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp; 以前的Dp都是利用前面得到的最优解来解决,而这题的麻烦点是MP在攻击过后要自动恢复x个单位[dan wei];用当前的状态[zhuang tai]的状态[zhuang tai]推下一状态[xia yi zhuang tai][zhuang tai],仔细想想也未尝不可;状态[zhuang tai]转移方程为:<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp; Dp[i+1][j-sk[k].mp+x]=min(Dp[i+1][j-sk[k].mp+x],Dp[i][j]+sk[k].at; 释放[shi fang]第K种技能,物理攻击可以看成是at=1,mp=0 的魔法;<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp;<br style="line-height: normal; " />Regular Words http://acm.hdu.edu.cn/showproblem.php?pid=1502&nbsp;<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp; F[a][b][c]=F[a-1][b][c]+F[a][b-1][c]+F[a][b][c-1];<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp; a&gt;=b&gt;=c;&nbsp;&nbsp;&nbsp;&nbsp;<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp;<br style="line-height: normal; " />Advanced Fruits http://acm.hdu.edu.cn/showproblem.php?pid=1503&nbsp;<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp; 最长公共子序列的加强版&nbsp;&nbsp;&nbsp;&nbsp;<br style="line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp; posted</div></td></tr></tbody></table></div><img src ="http://www.cppblog.com/yp0408100207/aggbug/167439.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/yp0408100207/" target="_blank">煙雨默嘫</a> 2012-03-08 20:38 <a href="http://www.cppblog.com/yp0408100207/archive/2012/03/08/167439.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>