Bookshelf 2
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 11105   Accepted: 4928

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

Source

【题意】:现有一个书架,N头牛,农场主想把这些牛放在书架上。但书架是有固定的高度的。现在问你在这些牛之中,其中一些牛的高度加在一块比书架高的最小差值是多少?
【分析】:

#include<stdio.h>
int cow[];
int b;
int n;
int ans=;
void DFS(int num,int sum)
{
if(sum>=ans) return;
if(num==n)
{
if(sum>=b) ans=sum;
return;
}
DFS(num+,sum);
DFS(num+,sum+cow[num]);
}
int main()
{
//freopen("input","r",stdin);
int i,j,cnt,tmp;
int sum;
scanf("%d %d",&n,&b);
for(i=;i<n;i++)
scanf("%d",&cow[i]);
DFS(,);
printf("%d",ans-b);
return ;
}

POJ 3628 Bookshelf 2【背包型DFS/选or不选】的更多相关文章

  1. POJ 3628 Bookshelf 2 0-1背包

    传送门:http://poj.org/problem?id=3628 题目看了老半天,牛来叠罗汉- -|||和书架什么关系啊.. 大意是:一群牛来叠罗汉,求超过书架的最小高度. 0-1背包的问题,对于 ...

  2. POJ 3628 Bookshelf 2(01背包)

    Bookshelf 2 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9488   Accepted: 4311 Descr ...

  3. POJ 3628 Bookshelf 2【01背包】

    题意:给出n头牛的身高,以及一个书架的高度,问怎样选取牛,使得它们的高的和超过书架的高度最小. 将背包容量转化为所有牛的身高之和,就可以用01背包来做=== #include<iostream& ...

  4. POJ 3628 Bookshelf 2 (01背包)

    Bookshelf 2 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7496   Accepted: 3451 Descr ...

  5. 洛谷 P1036 选数【背包型DFS/选or不选】

    题目描述 已知 n 个整数 x1,x2,…,xn,以及一个整数 k(k<n).从 n 个整数中任选 k 个整数相加,可分别得到一系列的和.例如当 n=4,k=3,4 个整数分别为 3,7,12, ...

  6. nyoj zb的生日【背包型DFS/选or不选】

    zb的生日 时间限制:3000 ms  |  内存限制:65535 KB 难度:2   描述 今天是阴历七月初五,acm队员zb的生日.zb正在和C小加.never在武汉集训.他想给这两位兄弟买点什么 ...

  7. poj 3628 Bookshelf 2 基本01背包

    题目大意:FJ有n头奶牛,和一个高为h的架子,给出每头奶牛高度,求使奶牛叠加起来超过架子的最低高度是多少. 题目思路:求出奶牛叠加能达到的所有高度,并用dp[]保存,最后进行遍历,找出与h差最小的dp ...

  8. poj 3628 Bookshelf 2

    http://poj.org/problem?id=3628 01背包 #include <cstdio> #include <iostream> #include <c ...

  9. POJ 3628 Bookshelf 2 题解

    本题解法非常多,由于给出的数据特殊性故此能够使用DFS和BFS,也能够使用01背包DP思想来解. 由于一般大家都使用DFS,这里使用非常少人使用的BFS.缺点是比DFS更加耗内存,只是长处是速度比DF ...

随机推荐

  1. JavaSE_12_序列化流和打印流

    1.1 序列化流概述 Java 提供了一种对象序列化的机制.用一个字节序列可以表示一个对象,该字节序列包含该对象的数据.对象的类型和对象中存储的属性等信息.字节序列写出到文件之后,相当于文件中持久保存 ...

  2. PHP函数高级(二)

    PHP函数基础:https://www.cnblogs.com/lxwphp/p/9867840.html   1.函数分类: 定义:完成某些功能的代码段 系统函数:字符串,数组,数字,日期时间 自定 ...

  3. 自定义UICollectionViewLayout之CATransform3D

    1.自定义UICollectionViewLayout旋转效果 之前有自定义UICollectionViewLayout(适用于多个section),本文是一个对cell进行CATransform3D ...

  4. ssh连接Ubuntu之access denied

    解决方法是: 修改/etc/ssh/ssh_config文件, #PermitRootLogin prohibit-password改为PermitRootLogin yes 然后 PasswordA ...

  5. 经典分类CNN模型系列其五:Inception v2与Inception v3

    经典分类CNN模型系列其五:Inception v2与Inception v3 介绍 Inception v2与Inception v3被作者放在了一篇paper里面,因此我们也作为一篇blog来对其 ...

  6. [51nod-1364]最大字典序排列

    [51nod-1364]最大字典序排列 Online Judge:51nod-1364 Label:线段树,树状数组,二分 题目描述 题解: 根据题意很容易想到60%数据的\(O(N^2logN)\) ...

  7. WINDOWS选择目录SHBrowseForFolder使用方法介绍

    首先介绍一个兼容Unicode和多字节的方法,定义如下头文件: // TString.h; #pragma once #include <string> #ifdef UNICODE ty ...

  8. rabbitmq类

    1.accept.php消费者代码需要在命令行执行 2.'username'=>'asdf','password'=>'123456' 改成自己的帐号和密码 RabbitMQCommand ...

  9. python使用SUDS调用webservice

    最近做接口对接,遇到了.net开发的webservice接口,因为python第一次与webservice对接,连问带查,最后使用suds库来实现了 1.安装suds mac: sudo pip in ...

  10. 最小费用最大流——EK+SPFA

    终于把最小费用最大流学会了啊-- 各种奇奇怪怪的解释我已经看多了,但在某些大佬的指点下,我终于会了. 原来是个好水的东西. 最小费用最大流是什么? 不可能不知道网络流吧?如果不知道,自行百度去-- 费 ...