Bookshelf 2
Time Limit: 1000MS   Memory Limit: 65536K

Description

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

解题思路:把书架的高度看做背包容量,将牛的高度看做物品的体积和价值,dp[i] 表示书架高度为i,牛的总高度不超过i的最大高度。

#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;
}

也可以用搜索来写:

#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 (01背包)相关推荐

  1. POJ 3628 Bookshelf 2

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

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

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

  3. POJ 3624 Charm Bracelet 0-1背包

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

  4. POJ 3624 Charm Bracelet(01背包 基础)

    题意: n个装饰品 容量m的背包 每个装饰品 重wi 价值 di 求能装的最大价值 思路:基础01背包 #include<stdio.h> #include<string.h> ...

  5. poj 1252 Euro Efficiency (01背包变形)

    http://poj.org/gotoproblem?pid=1252 (1)不是单纯的01背包问题,硬币有去有回(即有正有负),这使得用想买你的解法,上限难以确定,所以我开了dp[10000]的数组 ...

  6. poj 3624 Charm Bracelet (01背包)

    题目链接:http://poj.org/problem?id=3624 //典型01背包问题 //状态转移方程dp[i][j]=max(dp[i-1][j],dp[i-1][j-w[i]]+d[i]) ...

  7. POJ 1837 Balance(01背包变型)

    题意: 有一个杠杆,上面有c个钩子,有g件物品,全部放上去,问有多少种方式使天平平衡. 思路: 1. dp[i][j]代表第 i 件物品放到杆子上面之后,平衡度为 j 的方法数 2. 如果不加处理,j ...

  8. POJ - 3628 Bookshelf 2

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

  9. POJ 1837 Blance (01背包)

    Gigel has a strange "balance" and he wants to poise it. Actually, the device is different ...

最新文章

  1. 语义分割领域开山之作:Google提出用神经网络搜索实现语义分割
  2. 【Linux】 linux的进程系统一点补充
  3. ava返回json格式的状态码数据(一)
  4. 烟袋斜街-后海,印象已模糊
  5. hadoop之文件管理基本操作
  6. 机器分配(信息学奥赛一本通-T1266)
  7. myeclipse下使用Junit4进行单元测试
  8. HTML参考系列(1)-文本格式化标签
  9. paip.erlang 文本文件读写操作attilax总结
  10. 干货 | 基于JMP的方差分析及两两比较的思路与实现
  11. php漂浮广告代码,JS实现网站悬浮广告的代码
  12. [kuangbin带你飞]专题六 最小生成树
  13. 技术人员在客户现场工作注意事项
  14. 看不见的竞争 文件和数据压缩
  15. HTML5期末大作业:漫画网站设计——布卡漫画官网(4页) HTML+CSS+JavaScript 学生DW网页设计作业成品 w学生原创课程设计漫画设计制作大作业成品免费下载
  16. 某游戏客户流失情况数据分析
  17. Respond 的响应式代码阅读
  18. auc指标含义的理解
  19. Latex学习笔记(七)参考文献模板
  20. Python 进度条带时间

热门文章

  1. 阅读 Linux 内核源码——共享内存
  2. MyBatis知多少(22)MyBatis删除操作
  3. .NET技术学习目录整理
  4. PESTEL的风险建模
  5. number of databases available at XJTLU
  6. english email writing techniques demo
  7. 当孩子面对困难的时候,家人可能的鼓励的方式
  8. 浅谈iOS 开发中的界面通信
  9. goland 交叉生成linux文件
  10. jack对海量数据库的一些总结