POJ 3628 Bookshelf 2
POJ 3628 Bookshelf 2:http://poj.org/problem?id=3628
题意:有个书架,高度为B,现在FJ有N个奶牛,每个奶牛有个高度hi,现在将奶牛堆起来,使得堆起来的高度大于等于B,现在要求最小高度差。
思路一: 01背包:把奶牛的高度总和减去B,就是这个背包的容量,填充它,求出的最大值与背包的容量做差,结果就是他了。
CODE:
1 #include<stdio.h> 2 #include<string.h> 3 int w[100],f[20000000]; 4 int max(int a,int b) 5 { 6 return a>b?a:b; 7 } 8 int main() 9 { 10 int n,b,i,j; 11 int sum=0; 12 while(scanf("%d%d",&n,&b)!=EOF) 13 { 14 for(i=0;i<=n;i++) 15 f[i]=0; 16 sum=0; 17 for(i=0; i<n; i++) 18 { 19 scanf("%d",&w[i]); 20 sum+=w[i]; 21 } 22 int k=sum-b; 23 for(i=0; i<n; i++) 24 { 25 for(j=k; j>=w[i]; j--) 26 { 27 f[j]=max(f[j],f[j-w[i]]+w[i]); 28 } 29 } 30 printf("%d\n",k-f[k]); 31 } 32 return 0; 33 }
思路二:DFS:(转)
1 #include <iostream> 2 #include <cstdio> 3 #include <cstdlib> 4 #include <cstring> 5 using namespace std; 6 #define maxn 25 7 int n, m; 8 int f[maxn], s[maxn]; 9 int ans; 10 void dfs(int cow, int sum) 11 { 12 if (ans == 0) 13 return; 14 if (s[cow] + sum < m) 15 return; 16 if (sum >= m) 17 { 18 ans = min(ans, sum - m); 19 return; 20 } 21 if (cow == n) 22 return; 23 dfs(cow + 1, sum); 24 dfs(cow + 1, sum + f[cow]); 25 } 26 int main() 27 { 28 scanf("%d%d", &n, &m); 29 for (int i = 0; i < n; i++) 30 scanf("%d", &f[i]); 31 s[n] = 0; 32 for (int i = n - 1; i >= 0; i--) 33 s[i] = s[i + 1] + f[i]; 34 ans = 0x3f3f3f3f; 35 dfs(0,0); 36 printf("%d\n", ans); 37 return 0; 38 }
转载于:https://www.cnblogs.com/PJQOOO/p/3909285.html
POJ 3628 Bookshelf 2相关推荐
- POJ 3628 Bookshelf 2 (01背包)
Bookshelf 2 Time Limit: 1000MS Memory Limit: 65536K Description Farmer John recently bought anothe ...
- POJ - 3628 Bookshelf 2
Farmer John recently bought another bookshelf for the cow library, but the shelf is getting filled u ...
- 【 POJ - 3628 】Bookshelf 2(dfs 或 dp,0-1背包)
题干: Farmer John recently bought another bookshelf for the cow library, but the shelf is getting fill ...
- POJ 3627 Bookshelf 贪心 水~
最近学业上堕落成渣了.得开始好好学习了. 还有呀,相家了,好久没回去啦~ 还有和那谁谁谁... 嗯,不能发表悲观言论.说好的. 如果这么点坎坷都过不去的话,那么这情感也太脆弱. ----------- ...
- POJ 3268 Bookshelf 2 动态规划法题解
Description Farmer John recently bought another bookshelf for the cow library, but the shelf is gett ...
- POJ 3627 Bookshelf (贪心)
Description Farmer John recently bought a bookshelf for cow library, but the shelf is getting filled ...
- POJ 3627 Bookshelf
Bookshelf Time Limit: 1000MS Memory Limit: 65536K Description Farmer John recently bought a bookshel ...
- poj 3682 Bookshelf 2——背包问题(dfs)
Bookshelf 2 Description Farmer John recently bought another bookshelf for the cow library, but the s ...
- DP分类题目 转载 《志当存高远》大神的 没有冒犯的意思 只是拿过来学习的
A.各种背包,就根据背包九讲的内容来做吧! --不会背包的可以边看背包九讲边练习 =========================================================== ...
最新文章
- 连续地址数据(数组或者malloc的内存)作为函数参数
- crx header invalid_解决Chrome插件安装时程序包无效【CRX_HEADER_INVALID】(转载+xPath插件)...
- linux无任务内存被占满,centos服务器CPU被GC task占满,程序无内存溢出
- Wait--查看等待
- 第五届蓝桥杯省赛javaB组试题解析
- LeetCode 581. 最短无序连续子数组(Shortest Unsorted Continuous Subarray)
- Java03接口与内部类
- 计算机应用基础 辅助教学系统,计算机应用基础课程辅助教学及智能测评系统使用手册——网络版.docx...
- 前端开发 表格的补充 注释 标签的嵌套规则 0228
- concat特征融合_如何理解concat和add的方式融合特征
- 转 - Web新人(偏前端)应该怎样学习(个人观点,勿喷)
- AForge.net简介和认识
- WannaCry只是个开始?信息时代你急需的安全书单
- Linux下opengl性能更高,OpenGL超级宝典学习笔记——性能比较
- Java中tostring重写_使用ToStringBuilder重写toString方法
- 手机号段199/198/166,横空出世
- Hadoop完全分布式配置
- 正则 纳税号_正则表达式号码靓号类型判断代码
- jinjia2中for循环索引
- 树莓派python界面编程_树莓派PythonGUI学习
热门文章
- 虚拟与现实的距离——VR产业链史上最全梳理收藏版本【上篇】
- Github 简明教程
- 中国工程院院士高文:运用好云计算、大数据会为城市发展带来变革
- Kafka Zero-Copy 使用分析
- error: jump to label ‘XXXX’ [-fpermissive]
- Annotations
- GoCart 分类和产品 测试一
- Apache将整合Google Wave功能
- 很棒的在线裁图工具jQuery1.4.2 + jquery.bitmapcutter.js + BitmapCutter.Core+的完美配合
- 校招真题练习011 种花(美团)