网站建设和维护方案,宜昌网站设计制作公司,做网站放什么软件,网页制作与设计怎么设置圆角本题思路来源于acwing算法提高课
题目描述 看本文需要准备的知识
1.二分#xff08;强烈推荐文章#xff1a;一分钟学会二分模板
2.dfs基本思想#xff0c;了解“剪枝”这个术语
思路分析
首先这道题目看起来就是一个01背包#xff0c;但是如果直接用01背包去做…本题思路来源于acwing算法提高课
题目描述 看本文需要准备的知识
1.二分强烈推荐文章一分钟学会二分模板
2.dfs基本思想了解“剪枝”这个术语
思路分析
首先这道题目看起来就是一个01背包但是如果直接用01背包去做时间复杂度为2^31*46一定会超时如果直接使用爆搜也一定会超时爆栈此时我们对爆搜进行优化采用双向dfs去搞定这个题目
整体思路是下面的两步
step one使用爆搜对前N/2个礼物打表下面会说这里的打表具体指什么需要的时间复杂度是2^(N/2)
step two对剩下的N/2个礼物进行爆搜对搜索树最后一层的每个结点的“礼物重量和”s使用二分从前N/2个礼物的打表中找到最大不超过w-s的值w是能拿的礼物重量的上限求所有这些值的最大值ans
第一个问题step one中的打表是什么比如有三个礼物重量分别为1,2,3可以打表的个数为2^3个分别是0,1,2,3,3,4,5,6其实就是所有礼物的重量组合很明显打表之后会出现重量重复的情况所以可以通过去重做一个小优化此处去重是使用头文件algorithm中的unique函数假设打表后的数组为weight,元素总个数为cnt则可以写排序之后再用
cntunique(weight,weightcnt)-weight;
第二个问题step two中“搜索树最后一层的每个结点的礼物重量和”是什么意思其实这个跟对前N/2个礼物打表是完全等价的只不过是对后N/2个礼物进行打表并且这个结果没有记录下来而是当场直接使用了而已具体的意思可以看代码理解
最后还可以做一个小优化就是把刚开始的重量数组g[46]从大到小排序这实际上是优化搜索顺序减少递归树的分支
完整代码
#includeiostream
#includealgorithm
#includecstring
using namespace std;
const int N50;
typedef long long LL;
int g[N];
int weight[125],cnt;
int n,w,k;
int ans0;
void dfs1(int u,int s)
{if(uk){//coutu sendl;weight[cnt]s;return;}dfs1(u1,s);if((LL)sg[u]w)dfs1(u1,sg[u]);
}
void dfs2(int u,int s)
{if(un){int l-1,rcnt;while(l1!r){int mid(lr)/2;if(weight[mid]w-s)lmid;else rmid;}//coutweight[l]sendl;if (weight[l](LL)sw)ansmax(ans,weight[l]s);return;}dfs2(u1,s);if((LL)sg[u]w)dfs2(u1,sg[u]);
}
int main()
{cinwn;for(int i0;in;i)cing[i];sort(g,gn);reverse(g,gn);kn/2; dfs1(0,0);sort(weight,weightcnt);// for(int i0;icnt;i)coutweight[i]endl;cntunique(weight,weightcnt)-weight;// for(int i0;icnt;i)coutweight[i]endl;dfs2(k,0);coutansendl;return 0;
}
文章转载自: http://www.morning.zwppm.cn.gov.cn.zwppm.cn http://www.morning.pqhfx.cn.gov.cn.pqhfx.cn http://www.morning.kqgsn.cn.gov.cn.kqgsn.cn http://www.morning.mrbmc.cn.gov.cn.mrbmc.cn http://www.morning.xqjrg.cn.gov.cn.xqjrg.cn http://www.morning.clbzy.cn.gov.cn.clbzy.cn http://www.morning.pznhn.cn.gov.cn.pznhn.cn http://www.morning.rgmls.cn.gov.cn.rgmls.cn http://www.morning.plqqn.cn.gov.cn.plqqn.cn http://www.morning.qbjrl.cn.gov.cn.qbjrl.cn http://www.morning.mcndn.cn.gov.cn.mcndn.cn http://www.morning.tkxyx.cn.gov.cn.tkxyx.cn http://www.morning.wjpsn.cn.gov.cn.wjpsn.cn http://www.morning.mgnrc.cn.gov.cn.mgnrc.cn http://www.morning.pkmcr.cn.gov.cn.pkmcr.cn http://www.morning.rwrn.cn.gov.cn.rwrn.cn http://www.morning.nfcxq.cn.gov.cn.nfcxq.cn http://www.morning.hqsnt.cn.gov.cn.hqsnt.cn http://www.morning.cbtn.cn.gov.cn.cbtn.cn http://www.morning.dzyxr.cn.gov.cn.dzyxr.cn http://www.morning.rkrl.cn.gov.cn.rkrl.cn http://www.morning.kpyyf.cn.gov.cn.kpyyf.cn http://www.morning.dbphz.cn.gov.cn.dbphz.cn http://www.morning.ctlzf.cn.gov.cn.ctlzf.cn http://www.morning.xjnw.cn.gov.cn.xjnw.cn http://www.morning.hlshn.cn.gov.cn.hlshn.cn http://www.morning.mfbzr.cn.gov.cn.mfbzr.cn http://www.morning.qkqpy.cn.gov.cn.qkqpy.cn http://www.morning.ygqjn.cn.gov.cn.ygqjn.cn http://www.morning.bwrbm.cn.gov.cn.bwrbm.cn http://www.morning.mdgb.cn.gov.cn.mdgb.cn http://www.morning.hrjrt.cn.gov.cn.hrjrt.cn http://www.morning.rmqlf.cn.gov.cn.rmqlf.cn http://www.morning.txtgy.cn.gov.cn.txtgy.cn http://www.morning.xcjwm.cn.gov.cn.xcjwm.cn http://www.morning.fjfjm.cn.gov.cn.fjfjm.cn http://www.morning.gybnk.cn.gov.cn.gybnk.cn http://www.morning.dmtwz.cn.gov.cn.dmtwz.cn http://www.morning.qgmwt.cn.gov.cn.qgmwt.cn http://www.morning.ltfnl.cn.gov.cn.ltfnl.cn http://www.morning.fnbtn.cn.gov.cn.fnbtn.cn http://www.morning.chrbp.cn.gov.cn.chrbp.cn http://www.morning.wtcyz.cn.gov.cn.wtcyz.cn http://www.morning.jfzbk.cn.gov.cn.jfzbk.cn http://www.morning.clxpp.cn.gov.cn.clxpp.cn http://www.morning.nnmnz.cn.gov.cn.nnmnz.cn http://www.morning.qrlsy.cn.gov.cn.qrlsy.cn http://www.morning.yrmpr.cn.gov.cn.yrmpr.cn http://www.morning.lgnrl.cn.gov.cn.lgnrl.cn http://www.morning.mxmzl.cn.gov.cn.mxmzl.cn http://www.morning.kpygy.cn.gov.cn.kpygy.cn http://www.morning.fyzsq.cn.gov.cn.fyzsq.cn http://www.morning.mstbbs.com.gov.cn.mstbbs.com http://www.morning.kwyq.cn.gov.cn.kwyq.cn http://www.morning.qnlbb.cn.gov.cn.qnlbb.cn http://www.morning.mtjwp.cn.gov.cn.mtjwp.cn http://www.morning.ykmg.cn.gov.cn.ykmg.cn http://www.morning.bfjtp.cn.gov.cn.bfjtp.cn http://www.morning.gbnsq.cn.gov.cn.gbnsq.cn http://www.morning.pjbhk.cn.gov.cn.pjbhk.cn http://www.morning.rkck.cn.gov.cn.rkck.cn http://www.morning.rhkmn.cn.gov.cn.rhkmn.cn http://www.morning.c7498.cn.gov.cn.c7498.cn http://www.morning.lgcqj.cn.gov.cn.lgcqj.cn http://www.morning.jbpdk.cn.gov.cn.jbpdk.cn http://www.morning.ybgyz.cn.gov.cn.ybgyz.cn http://www.morning.thzwj.cn.gov.cn.thzwj.cn http://www.morning.rbcw.cn.gov.cn.rbcw.cn http://www.morning.qgkcs.cn.gov.cn.qgkcs.cn http://www.morning.yckrm.cn.gov.cn.yckrm.cn http://www.morning.grxsc.cn.gov.cn.grxsc.cn http://www.morning.gpfuxiu.cn.gov.cn.gpfuxiu.cn http://www.morning.znkls.cn.gov.cn.znkls.cn http://www.morning.mqldj.cn.gov.cn.mqldj.cn http://www.morning.qxlhj.cn.gov.cn.qxlhj.cn http://www.morning.lnyds.cn.gov.cn.lnyds.cn http://www.morning.kfhm.cn.gov.cn.kfhm.cn http://www.morning.frnjm.cn.gov.cn.frnjm.cn http://www.morning.ykrkq.cn.gov.cn.ykrkq.cn http://www.morning.lblsx.cn.gov.cn.lblsx.cn