POJ3628 Bookshelf 2(01背包+dfs)
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 8745 | Accepted: 3974 |
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个数中和比减16最小的。
5+6+3+3 - 16 = 1最小的是1;
01背包,背包的容量是所有的数累加起来的和sum,想想为什么呢,可以累加起来的和一定比给的那个数大,然后就从b-sum查找背包中的比b大的第一个数就ok了,
很棒的转换
dfs也能轻松搞定
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
using namespace std;
const int INF = 0x3f3f3f3f;
const int MAX = + ;
int dp[MAX],sum,a[MAX]; int main()
{
int n,b;
while(scanf("%d%d", &n,&b) != EOF)
{
for(int i = ; i <= n; i++)
{
scanf("%d", &a[i]);
sum += a[i];
}
memset(dp, , sizeof(dp));
for(int i = ; i <= n; i ++)
{
for(int j = sum; j >= a[i]; j--)
{
dp[j] = max(dp[j],dp[j - a[i]] + a[i]);
}
}
for(int i = b; i <= sum; i++)
{
if(dp[i] >= b)
{
printf("%d\n",dp[i] - b);
break;
}
} } return ;
}
POJ3628 Bookshelf 2(01背包+dfs)的更多相关文章
- [Swust OJ 465]--吴奶奶买鱼(0-1背包+dfs)
题目链接:http://acm.swust.edu.cn/problem/465/ 还有一道题只是描述不一样,方法一模一样(http://acm.swust.edu.cn/problem/644/) ...
- hdu3448 01背包+dfs
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=3448 Description 0/1 bag problem should sound f ...
- HDU_2079_(01背包)(dfs)
选课时间(题目已修改,注意读题) Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- POJ 3628 Bookshelf 2 0-1背包
传送门:http://poj.org/problem?id=3628 题目看了老半天,牛来叠罗汉- -|||和书架什么关系啊.. 大意是:一群牛来叠罗汉,求超过书架的最小高度. 0-1背包的问题,对于 ...
- codeforces 842C Ilya And The Tree (01背包+dfs)
(点击此处查看原题) 题目分析 题意:在一个树中,有n个结点,记为 1~n ,其中根结点编号为1,每个结点都有一个值val[i],问从根结点到各个结点的路径中所有结点的值的gcd(最大公约数)最大是多 ...
- POJ 3628 Bookshelf 2 (01背包)
Bookshelf 2 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7496 Accepted: 3451 Descr ...
- Bookshelf 2 01背包
B - Bookshelf 2 Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu Submi ...
- POJ3628:Bookshelf 2【01背包】
Description Farmer John recently bought another bookshelf for the cow library, but the shelf is gett ...
- Bookshelf 2(poj3628,01背包,dp递推)
题目链接:Bookshelf 2(点击进入) 题目解读: 给n头牛,给出每个牛的高度h[i],给出一个书架的高度b(所有牛的高度相加>书架高度b),现在把一些牛叠起来(每头牛只能用一次,但不同的 ...
随机推荐
- 介绍Git版本控制器的使用
Git 简介 Git 是什么?大家肯定会说不就是版本控制器嘛,是的Git是目前世界上最先进的分布式版本控制系统(没有之一). 1.那什么是版本控制器呢? 举个简单的例子,比如我们用Word写文章,那你 ...
- 1140 Jam的计数法
1140 Jam的计数法 2006年NOIP全国联赛普及组 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题解 查看运行结果 题目描述 Descri ...
- 01JavaIO详解_File类
对程序语言设计者来说,设计一个令人满意的I/O系统,是件极艰难的任务.——摘自Think in java 对java而言,File表示的是文件或目录.但是我们知道文件和目录是不一样的,文件里面存放的是 ...
- 错题802-java
1.list是一个ArrayList的对象,哪个选项的代码填到//todo delete处,可以在Iterator遍历的过程中正确并安全的删除一个list中保存的对象?() Iterator it = ...
- OV7725学习(二)
首先要配置OV7725摄像头的寄存器,遵循的是SCCB协议,配置之前需要1ms的时间等待,保证系统稳定,而且刚开始要丢弃前10帧的数据,因为认为前10帧的数据是不稳定的,图1就是数据手册上关于这一点的 ...
- C# 结构体
1,结构体不能出现在继承关系中,除了继承接口. 结构体不能继承类或结构,也不能被类或结构继承,只可以继承接口. 2,struct不能定义默认构造函数(无参构造函数),也不能定义析构函数.class对这 ...
- windows 7 安装 scrapy
基于64位 win7 系统 先到 http://www.lfd.uci.edu/~gohlke/pythonlibs/ 下载四个 wheel 文件: 1. lxml-3.4.4-cp27-none-w ...
- JS案例之6——瀑布流布局(1)
在实际的项目中,偶尔会用到一种布局——瀑布流布局.瀑布流布局的特点是,在多列布局时,可以保证内容区块在水平方向上不产生大的空隙,类似瀑布的效果.简单的说,在垂直列表里,内容区块是一个挨着一个的.当内容 ...
- Android ViewPager使用详解
这是谷歌官方给我们提供的一个兼容低版本安卓设备的软件包,里面包囊了只有在安卓3.0以上可以使用的api.而viewpager就是其中之一利用它,我们可以做很多事情,从最简单的导航,到页面菜单等等.那如 ...
- [CareerCup] 13.3 Virtual Functions 虚函数
13.3 How do virtual functions work in C++? 这道题问我们虚函数在C++中的工作原理.虚函数的工作机制主要依赖于虚表格vtable,即Virtual Table ...