bzoj 1082: [SCOI2005]栅栏 (二分+dfs)
来源:互联网 发布:陈坤孩子的母亲知乎 编辑:程序博客网 时间:2024/06/06 18:29
1082: [SCOI2005]栅栏
Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1942 Solved: 826
[Submit][Status][Discuss]
Description
农夫约翰打算建立一个栅栏将他的牧场给围起来,因此他需要一些特定规格的木材。于是农夫约翰到木材店购
买木材。可是木材店老板说他这里只剩下少部分大规格的木板了。不过约翰可以购买这些木板,然后切割成他所需
要的规格。而且约翰有一把神奇的锯子,用它来锯木板,不会产生任何损失,也就是说长度为10的木板可以切成长
度为8和2的两个木板。你的任务:给你约翰所需要的木板的规格,还有木材店老板能够给出的木材的规格,求约翰
最多能够得到多少他所需要的木板。
Input
第一行为整数m(m<= 50)表示木材店老板可以提供多少块木材给约翰。紧跟着m行为老板提供的每一块木板的长
度。接下来一行(即第m+2行)为整数n(n <= 1000),表示约翰需要多少木材。接下来n行表示他所需要的每一块木板
的长度。木材的规格小于32767。(对于店老板提供的和约翰需要的每块木板,你只能使用一次)。
Output
只有一行,为约翰最多能够得到的符合条件的木板的个数。
Sample Input
4
30
40
50
25
10
15
16
17
18
19
20
21
25
24
30
30
40
50
25
10
15
16
17
18
19
20
21
25
24
30
Sample Output
7
HINT
25切出 21 30切出 20 40切出 19、18 50切出 15、16、17
Source
题解:二分+dfs
要求尽可能满足多的木材,所有一定是尽可能选取小的来满足,也就是满足的一定是前k小,那么我们将需要的木材按照从小到大的顺序排序,然后二分最多能满足多少木材,判定的时候用dfs,如果只是单纯的dfs求解是否有可行方案一定会TLE。所以考虑剪枝,我们计算一个最大的浪费值(就是浪费值超过这个值就无解),然后利用这个来剪枝就可以了。
#include<iostream>#include<cstdio>#include<algorithm>#include<cstring>#include<cmath>#define N 1003using namespace std;int a[N],len[N],n,m,sum[N],lentot,waste;int cmp(int x,int y){return x>y;}bool dfs(int k,int w,int now){if (k==0) return true; bool pd=false;for (int i=now;i<=n;i++) if(len[i]>=a[k]) { len[i]-=a[k]; int t=w; if (len[i]<a[1]) t+=len[i]; if (t>waste) { len[i]+=a[k]; continue; }int last=0;if (a[k]==a[k-1]) last=now;else last=1; if (dfs(k-1,t,last)) pd=true; len[i]+=a[k]; if (pd) return true; }return false;}int main(){freopen("a.in","r",stdin);//freopen("my.out","w",stdout);scanf("%d",&n);for (int i=1;i<=n;i++) scanf("%d",&len[i]),lentot+=len[i];scanf("%d",&m);for (int i=1;i<=m;i++) scanf("%d",&a[i]);sort(a+1,a+m+1);for (int i=1;i<=m;i++) sum[i]=sum[i-1]+a[i];//cout<<sum[m]<<" "<<lentot<<endl;sort(len+1,len+n+1,cmp); int l=0; int r=m; int ans=0; while (a[l+1]<=len[l+1]&&l+1<=m) l++; ans=l; while (a[1]>len[n]&&n) lentot-=len[n],n--; while (a[m]>len[1]&&m) m--; r=m; //cout<<n<<" "<<m<<endl; while (l<=r){ int mid=(l+r)/2; waste=lentot-sum[mid]; //cout<<waste<<endl; if (dfs(mid,0,1)) ans=max(ans,mid),l=mid+1; else r=mid-1;}printf("%d\n",ans);}
0 0
- bzoj 1082: [SCOI2005]栅栏 (二分+dfs)
- bzoj 1082: [SCOI2005]栅栏(二分+DFS)
- BZOJ 1082 [SCOI2005]栅栏 二分+DFS
- BZOJ 1082: [SCOI2005]栅栏 DFS,剪枝,二分答案
- [SCOI2005]栅栏 二分+dfs
- 1082: [SCOI2005]栅栏 二分+dfs+剪枝
- [bzoj1082][SCOI2005]栅栏(二分+dfs剪枝)
- 【bzoj 1082】栅栏(二分+DFS)
- 【BZOJ 1082】 [SCOI2005]栅栏
- [bzoj] 1082: [SCOI2005]栅栏
- (bzoj 1082)(SCOI2005)栅栏
- BZOJ 1082: [SCOI2005]栅栏|二分答案爆搜
- BZOJ 1082 栅栏 (二分 剪枝搜索)
- BZOJ 1082 栅栏 [二分+搜索]
- 【bzoj1082】 SCOI2005 栅栏 二分+搜索
- bzoj 1082 [SCOI2005]栅栏 搜索姿势不对毁一生
- 1082: [SCOI2005]栅栏
- 栅栏的木料(二分+dfs)
- LeetCode No.40 Combination Sum II
- Android自定义View(二)——常用工具
- 【Android】GreenDao 3.X 结合Volley以及Gson、ImageLoader实现数据存储
- 洛谷 P1852 奇怪的字符串
- Java实现DES加密解密
- bzoj 1082: [SCOI2005]栅栏 (二分+dfs)
- Map接口的再学习
- 深入理解Java内存(图解)
- javascript基础笔记(十四)之js的全局函数
- java--java连接数据库
- Unsupported major.minor version 51.0解决办法
- Linux用户管理的笔记
- Set接口的再学习
- crate源码