Bookshelf 2

POJ - 3628

Farmer John recently bought another bookshelf for the cow library, but the shelf is getting filled up quite quickly, and now the only available space is at the top.

FJ has N cows (1 ≤ N ≤ 20) each with some height of Hi (1 ≤ Hi ≤ 1,000,000 - these are very tall cows). The bookshelf has a height of B (1 ≤ BS, where S is the sum of the heights of all cows).

To reach the top of the bookshelf, one or more of the cows can stand on top of each other in a stack, so that their total height is the sum of each of their individual heights. This total height must be no less than the height of the bookshelf in order for the cows to reach the top.

Since a taller stack of cows than necessary can be dangerous, your job is to find the set of cows that produces a stack of the smallest height possible such that the stack can reach the bookshelf. Your program should print the minimal 'excess' height between the optimal stack of cows and the bookshelf.

Input

* Line 1: Two space-separated integers: N and B
* Lines 2..N+1: Line i+1 contains a single integer: Hi

Output

* Line 1: A single integer representing the (non-negative) difference between the total height of the optimal set of cows and the height of the shelf.

Sample Input

5 16
3
1
3
5
6

Sample Output

1

代码:搜索

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int digit[25];
int book[25];
int gg[25];
typedef long long ll;
int n,b;
int inf=0x3f3f3f3f;
void DFS(int cnt,int sum)
{if(sum+gg[n]-gg[cnt-1]<b)return;if(sum>=b){inf=min(inf,sum);return;}DFS(cnt+1,sum);DFS(cnt+1,sum+digit[cnt]);return;
}
int main()
{   scanf("%d%d",&n,&b);for(int i=1;i<=n;i++){scanf("%d",&digit[i]);gg[i]=gg[i-1]+digit[i];}   DFS(1,0);printf("%d\n",inf-b);return 0;
}

代码:DP

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int digit[25];
typedef long long ll;
int n,b;
int sum=0;
int inf=0x3f3f3f3f;
int dp[1000000*20+5];
int main()
{   scanf("%d%d",&n,&b);for(int i=1;i<=n;i++){scanf("%d",&digit[i]);sum=sum+digit[i];} for(int i=1;i<=n;i++){for(int j=sum;j>=digit[i];j--){dp[j]=max(dp[j],dp[j-digit[i]]+digit[i]); }}for(int i=1;i<=sum;i++){if(dp[i]>=b){cout<<dp[i]-b<<endl;break;}}return 0;
}

Bookshelf 2 POJ - 3628(01背包||DFS)相关推荐

  1. 【 POJ - 3628 】Bookshelf 2(dfs 或 dp,0-1背包)

    题干: Farmer John recently bought another bookshelf for the cow library, but the shelf is getting fill ...

  2. POJ 3628 Bookshelf 2

    POJ 3628 Bookshelf 2:http://poj.org/problem?id=3628 题意:有个书架,高度为B,现在FJ有N个奶牛,每个奶牛有个高度hi,现在将奶牛堆起来,使得堆起来 ...

  3. poj 2063 Investment(01背包变形)

    http://poj.org/gotoproblem?pid=2063 (1)上限 m 一直上升的 n 次01背包问题,比一般的01背包多了一重循环: (2)本题出现了各种错误:1)刚开始,没注意 m ...

  4. bzoj2287【POJ Challenge】消失之物 缺一01背包

    bzoj2287[POJ Challenge]消失之物 缺一01背包 链接 bzoj 思路 分治solve(l,r,arr)表示缺少物品\([l,r]\)的dp数组arr. 然后solve(l,mid ...

  5. 【POJ - 3211】Washing Clothes (dp,0-1背包中点问题)

    题干: Dearboy was so busy recently that now he has piles of clothes to wash. Luckily, he has a beautif ...

  6. 【洛谷 - U43391】不是0-1背包的暴力AC(思维,二分,可转化为二元组问题,复习暴力dfs总结)

    题干: https://www.luogu.org/problemnew/show/U43391 自01背包问世之后,小A对此深感兴趣.一天,小A去远游,却发现他的背包不同于01背包. 小A的背包最多 ...

  7. LeetCode 1774. 最接近目标价格的甜点成本(DFS / 01背包)

    文章目录 1. 题目 2. 解题 1. 题目 你打算做甜点,现在需要购买配料.目前共有 n 种冰激凌基料和 m 种配料可供选购.而制作甜点需要遵循以下几条规则: 必须选择 一种 冰激凌基料. 可以添加 ...

  8. POJ3628:Bookshelf 2【01背包】

    Description Farmer John recently bought another bookshelf for the cow library, but the shelf is gett ...

  9. POJ 3624 Charm Bracelet 0-1背包

    传送门:http://poj.org/problem?id=3624 题目大意:XXX去珠宝店,她需要N件首饰,能带的首饰总重量不超过M,要求不超过M的情况下,使首饰的魔力值(D)最大. 0-1背包入 ...

  10. hdoj 1864 最大报销额 【01背包】||【dfs】

    最大报销额 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Subm ...

最新文章

  1. 还缺30万人!程序员2020年要过好日子了……
  2. outlook2007 未知错误,代码0x80040600解决方法
  3. 全球最强大脑在哪?阿里小蜜算法团队在寻找
  4. 设计模式:单一职责原则
  5. python判断性别程序_python 实现性别识别
  6. Linux驱动总结3- unlocked_ioctl和堵塞(waitqueue)读写函数的实现
  7. 3.netwox网络工具集入门教程
  8. 服务器全息显示修改,全息显示
  9. dms系统与mysql_DMS管理MYSQL数据库登陆不了。
  10. 今晚7:00 | PhD Debate 自监督学习在推荐系统中的应用
  11. HTML5中thead标签,【HTML5】table标签中thead、tbody、tfoot的作用
  12. 硬盘数据恢复方法有哪些?希望这些方法能帮助你
  13. 快手第三季营收231亿:同比增13% 期内亏损27亿
  14. Docker教程(二):docker常用命令
  15. jxcell开发简单实例
  16. scala trait ,abstract class,case class,class 的一些琐碎注意事项,仅针对3.0以下
  17. Android之头像图片变圆形
  18. 考研OR工作----计算机操作系统简答题及疑难知识点总结(第一章 操作系统引论)
  19. mybatisplus sql 改写2
  20. Akka(二):使用Akka模拟yarn

热门文章

  1. APICS与AX的Master Planning(一)--Phantom bill of Material 虚项
  2. Java经典设计模式(1):五大创建型模式(附实例和详解)
  3. 前端:用css打造炫酷3d特效- css3d立方体
  4. 2_C语言中的数据类型 (一)2.1.常量和字符串常量
  5. Qt--将Qt 动态链接生成的exe及依赖dll打包方法
  6. openstack Essex中nova-volume
  7. svn里的branch、trunk、tag的用处
  8. Windows 無法查詢{7B849a69-220F-......} 的 DllName 登錄項目
  9. 定制.NET GridView的长文本显示表格
  10. jvm系列:Java服务GC参数调优案例