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

  1. 5 16
  2. 3
  3. 1
  4. 3
  5. 5
  6. 6

Sample Output

  1. 1
  2.  
  3. 思路:读题读的头大,基础dp问题,01背包,dp[i][j]表示前i只牛,牛的高度为j,其能达到书架的最高高度,转移方程为选/不选第i只牛,dp[i][j]=max(dp[i-1][j],dp[i-1][j-h[i]]+h[i])
  1. const int maxm = 2e7+;
  2.  
  3. int dp[maxm], sum, buf[];
  4.  
  5. int main() {
  6. ios::sync_with_stdio(false), cin.tie();
  7. int N, B;
  8. cin >> N >> B;
  9. for(int i = ; i <= N; ++i) {
  10. cin >> buf[i];
  11. sum += buf[i];
  12. }
  13. for(int i = ; i <= N; ++i) {
  14. for(int j = sum; j >= buf[i]; --j)
  15. dp[j] = max(dp[j], dp[j-buf[i]]+buf[i]);
  16. }
  17. for(int i = ; i <= sum; ++i)
  18. if(dp[i] >= B) {
  19. cout << dp[i] - B << endl;
  20. break;
  21. }
  22.  
  23. return ;
  24. }

Day9 - C - Bookshelf 2 POJ - 3628的更多相关文章

  1. poj 3628 Bookshelf 2

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

  2. POJ 3628 Bookshelf 2 0-1背包

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

  3. POJ 3628 Bookshelf 2(01背包)

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

  4. POJ 3628 Bookshelf 2 (01背包)

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

  5. POJ 3628 Bookshelf 2【背包型DFS/选or不选】

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

  6. POJ 3628 Bookshelf 2【01背包】

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

  7. poj 3628 Bookshelf 2 基本01背包

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

  8. POJ 3628 Bookshelf 2 题解

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

  9. poj 3628 (搜索or背包)

    好久没看背包题目了!!!生疏了!!!! 这题是背包题!!!不过对于这题,解决方法还是搜索省时!!! 题意:第一行给你一个N和VV,接下来N行,每行一个数,求得是任选N个数组合求和,求组合的和大于VV而 ...

随机推荐

  1. 基于 VS2019 配置 opencv4.x

    创建新项目 添加主函数文件 配置 注意,如果直接使用项目的属性去配置,那么创建新的项目的时候,还需要再配置一遍,在属性管理器里配置,创建新项目的时候,会自动应用 接下来,开始为软件配置目录和附加项.右 ...

  2. Django安装数据库MySQLdb

    在使用Django搭建与树莓派智能终端时,使用mysql作为数据库管理,遇到如下问题: django.core.exceptions.ImproperlyConfigured: Error loadi ...

  3. NOIP2016换教室 BZOJ 4720

    BZOJ 4720 换教室 题目描述: 对于刚上大学的牛牛来说,他面临的第一个问题是如何根据实际情况申请合适的课程.在可以选择的课程中,有2n节 课程安排在n个时间段上.在第i(1≤i≤n)个时间段上 ...

  4. excel 动太生成sql 复制(ctrl+c)即可使用 【即用excel中concatenate函数拼接sql】

    如图所示:有test_user表 id(自增).code.name.age.create_time几个字段. 使用函数:=CONCATENATE("insert into test_user ...

  5. 一个IP与多个域名绑定

    方法一:通过端口区分 方法二:通过serverName区分(推荐) 1.httpd.conf打开 virtual host虚拟主机配置 2.httpd-vhost.conf 添加配置 <Virt ...

  6. 设计模式01 创建型模式 - 原型模式(Protype Pattern)

    参考 1. 设计模式:原型模式 | 博客园 2. Java clone深拷贝.浅拷贝 | CSDN 3. Cloneable接口和Object的clone()方法 | 博客园 原型模式(Prototy ...

  7. github日常的基本命令

    git 常用命令 git clone 仓库地址 -从远端克隆项目 git pull -从远端拉取代码 git pull -p -从远端拉取代码和分支 提交代码流程: git add xxx -添加到暂 ...

  8. 学习笔记(11)- 文本生成RNNLG

    https://github.com/shawnwun/RNNLG 数据集 给出了4个行业的语料,餐馆.酒店.电脑.电视,及其组合数据. 数据格式 任务 根据给定格式的命令,生成自然语言. 方法.模型 ...

  9. android下创建文件夹和修改其权限的方法

    原文:http://www.cnblogs.com/wanqieddy/archive/2011/12/28/2304906.html 由于工作的需要,今天研究了在android下创建文件夹和修改其权 ...

  10. 劫后余生--New Start

    被搁置的计划 It was the best of times,it was the worst of times,it was the age of wisidom,it was the age o ...