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. Django之深入了解模板层

    目录 模板语法 模板传值 过滤器 标签 自定义过滤器和标签 模板继承 模板导入 模板语法 前端模板的语法只记住两种就行了. {{ xxx }} 变量相关的 { % % } 逻辑相关的 模板传值 我们通 ...

  2. Python3.6爬虫+Djiago2.0+Mysql --运行djiago环境

    1.安装djiago 模块 pip install Django  --默认安装最新的  安装完成以后可以python -m pip list 查看模块是否安装 2.创建项目及app 及生成目录 备注 ...

  3. 移动端自定义输入框的vue组件 ----input

    <style scoped lang="less"> .keyboard { font-family: -apple-system, BlinkMacSystemFon ...

  4. PAT甲级——A1094 The Largest Generation

    A family hierarchy is usually presented by a pedigree tree where all the nodes on the same level bel ...

  5. 《DSP using MATLAB》Problem 7.38

    代码: %% ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ %% Output In ...

  6. Spring boot获取yml字段内容为null的各种情况

    首先,在resource目录下配置test.yml文件 A: B: http://123.com? C: username="lili"&password="12 ...

  7. 11-2-break

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  8. 02-认识js

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  9. STL与泛型编程第一周作业

    /* 题目: 给定一个 vector:v1 = [0, 0, 30, 20, 0, 0, 0, 0, 10, 0],希望通过not_equal_to 算法找到到不为零的元素,并复制到另一个 vecto ...

  10. HBase Ambari