题干:

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 ≤ B ≤ S, 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

题目大意:

其实很简单啦就是给你n个高度,让你挑其中的几个摞起来。要求摞起来以后的总高度超过b,问你这样的高度的最小值是多少。(让你输出  最小值 - b )

有n头牛,已知每头牛的高度和书架高度,一头牛可以站在另一头牛身上,总高度是他们的高度之和。要求能够达到书架的顶端,即这些牛的总高度不低于书架高度,求满足条件的总高度的最小值,输出他们的差值。

解题报告:

这题高度小于100W(1e6),但是题目说b要小于总高度和,n<=20,也就是说b<=2000W(2e7),这个数字其实做0-1背包就已经很牵强了(我也试过了开了2e7的数组然后MLE了),不过还好这题数据量没给到这么大,开个100W的数组就过去了。不过要说到正解,这题是搜索啊!n<=20这么小的数据量你不去dfs吗?!

下面附四个代码:

AC代码1:(自己写  装满类型0-1背包)

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>using namespace std;
const int MAX = 1000000 +5;
const int INF = 0x3f3f3f3f;
int dp[MAX];
int v[50];bool cmp(const int a,const int b) {return a>b;
}
int main()
{int n,b;cin>>n>>b;for(int i = 1; i<=n; i++) {cin>>v[i];}sort(v+1,v+n+1,cmp);//cout <<"kaishile"<<endl;memset(dp,INF,sizeof(dp));dp[0] = 0;for(int i = 1; i<=n; i++) {for(int j = b + v[1]; j>=v[i]; j-- ) {dp[j] = min(dp[j],dp[j -v[i] ] + 1);}}int ans = -1;for(int i = b; i<=b+v[1]; i++) {if(dp[i] !=INF) {ans = i - b; break;}}cout << ans<<endl;return 0 ;
}

AC代码2:(网络版的 装满类型0-1背包)

#include<cstdio>
using namespace std;
const int mm=20000000;
bool f[mm];
int h[22];
int i,j,k,n,b,m;
int main()
{while(scanf("%d%d",&n,&b)!=-1){for(m=i=0;i<n;++i)scanf("%d",&h[i]),m+=h[i];for(i=0;i<=m;++i)f[i]=0;f[0]=1;for(i=0;i<n;++i)for(j=m-h[i];j>=0;--j)if(f[j])f[j+h[i]]=1;for(i=b;i<=m;++i)if(f[i])break;printf("%d\n",i-b);}return 0;
}

上面这个代码看起来0-1背包不太一样,其实是一样的。只不过一般j从m开始递减,这里是从m-h[i]开始的,其实是一样的。

之所以要把这个代码贴上来其实主要不是这个地方,而是他只用了bool类型的dp数组,因为这里只需要记录这个高度是否可以到达,而不需要知道具体能到达的高度是多少。其实往深里讲,这是因为这个题只有h[i] 这一个权值,而不是标准的0-1背包还需要w和v两个权值,所以其实就算是按照我上面自己写的代码那样写,其实每个dp数组中只要不是-INF,那么这个值一定就是高度一定就是i(也就是dp[i]一定等于i),所以其实这两种写法是等价的。

AC代码3:(纯裸的0-1背包)

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int dp[2000002], h[22];
int main()
{int n, m, i, j;while(~scanf("%d%d",&n,&m)){int sum = 0;memset(dp,0,sizeof(dp));for(i = 1; i <= n; i++){scanf("%d",&h[i]);sum += h[i];}for(i = 1; i <= n; i++)for(j = sum; j >= h[i]; j--)dp[j] = max(dp[j], dp[j - h[i]] + h[i]);int Min = sum;for(i = m; i <= sum; i++)if(dp[i] >= m && dp[i] - m < Min)Min = dp[i] - m;printf("%d\n",Min);}return 0;
}

AC代码4:(网络版 dfs)

#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
int h[22], ans, flag;
int n, m;
void dfs(int k, int s)
{if(s == m){ans = 0;return ;}if(s >= m){if(s - m < ans)ans = s - m;return ;}for(int i = k; i < n; i++){dfs(i+1,s+h[i]);}
}
int main()
{int i;while(cin >> n >> m){int sum = 0;flag = 0;for(i = 0; i < n; i++){cin >> h[i];sum += h[i];}if(sum == m){cout << "0" << endl;continue;}ans = sum;dfs(0,0);cout << ans << endl;}return 0;
}

【 POJ - 3628 】Bookshelf 2(dfs 或 dp,0-1背包)相关推荐

  1. POJ 3628 Bookshelf 2

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

  2. POJ 3628 Bookshelf 2 (01背包)

    Bookshelf 2 Time Limit: 1000MS   Memory Limit: 65536K Description Farmer John recently bought anothe ...

  3. POJ - 3628 Bookshelf 2

    Farmer John recently bought another bookshelf for the cow library, but the shelf is getting filled u ...

  4. 【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 ...

  5. dp(0,1背包)-----高数Umaru系列(9)——哈士奇

    高数Umaru系列(9)--哈士奇 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 由于高数巨养的喵星人太傲娇了,要天天 ...

  6. 【POJ - 3624 】Charm Bracelet (dp,0-1背包裸题)

    题干: Bessie has gone to the mall's jewelry store and spies a charm bracelet. Of course, she'd like to ...

  7. NOJ 1401 乘车费用 (线性dp 0/1背包)

    乘车费用 时间限制(普通/Java):1000MS/3000MS         运行内存限制:65536KByte 描述 lqp家离学校十分十分远,同时他又没有钱乘taxi.于是他不得不每天早早起床 ...

  8. poj 3682 Bookshelf 2——背包问题(dfs)

    Bookshelf 2 Description Farmer John recently bought another bookshelf for the cow library, but the s ...

  9. [poj 1014]Dividing的DFS解法解读和DP解法

    转载来源: http://blog.csdn.net/lyy289065406/article/details/6661449 这道题比较特殊,用DFS也是对的,而且可以进行优化,即使直接n[i]-- ...

最新文章

  1. 图像配准----RANSAC
  2. android 滑动顶部固定,android view滑动到顶部悬停
  3. sizeof 使用注意
  4. python创建excel图表_python使用VBA(8):Excel创建图表(简单)
  5. 齐二TK6916/20/26/32系列数控落地铣镗床简介8
  6. 灰色市场(Gray Market),简称灰市,也称半黑市
  7. 超级计算机的英语作文80字,英语作文_为什么这台超级计算机如此快? (有声) _沪江英语...
  8. CF573D-Bear and Cavalry【动态dp】
  9. 电商无线端秋季促销PSD分层海报,大战之前设计师准备好了么?
  10. Android 为TextView 添加边框
  11. 【BZOJ-1502】月下柠檬树 计算几何 + 自适应Simpson积分
  12. vivado 一次性设置多线程编译
  13. 在网页中使用超链接来打开本地应用(可传参数)
  14. 2017下半年,一二线互联网公司Android面试题汇总
  15. 一张图搞懂美国大数据产业(上)
  16. jdk 动态代理基本例子
  17. RESTful设计风格
  18. 零代码工具推荐 八爪鱼采集器
  19. 这篇不讨好任何人的回忆录,记录了我从双非学校到BAT/TMD六offer的原因
  20. 央行房贷新政难落地:上海四大行按基准利率执行

热门文章

  1. 关于递归函数,非递归函数中引用传参的问题
  2. pwm控制舵机转动角度程序_Mixly 第15课 舵机的使用
  3. phpstudy(自己电脑主机做服务器,手机网站界面打不开)
  4. oracle12 扩充表空间,oracle查询及扩充表空间
  5. java中write方法报错_Java中管道报错:Write end dead
  6. php 游标 上移,jQuery点击input使光标移动到最后或指定位置
  7. android eclipse services,Eclipse中调试Android Framework本地代码模块部分过程记录
  8. VXWORKS 几种定时机制
  9. android画布demo,Android开发画板demo前奏
  10. 计算机指令int,汇编入门学习笔记 (十二)—— int指令、端口