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头牛一个书架,书架太高要站在牛身上够书架,已知N头牛的高度和书架的高度,一头牛可以站在另一头牛身上,总高度是他们的高度之和,高度和需要不小于(大于等于均可)书架高度,问符合要求的最低高度是多少,输出该高度与书架高度的差值。
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define N 1005000
#define oo 0x3f3f3f
int a[N];
int dp[N];
int vis[N];
int n,p;
int main()///dp储存牛的高度和,背包容量 v 为所有牛的高度总和,价值和体积就是每头牛的高度。

{int t;int n,b;while(scanf("%d%d",&n,&b)!=EOF){int sum = 0;memset(a,0,sizeof(a));for(int i=1;i<=n;i++){scanf("%d",&a[i]);sum += a[i];}int Min = oo;for(int i=1;i<=n;i++){for(int j=sum;j>=a[i];j--){dp[j] = max(dp[j],dp[j-a[i]]+a[i]);if(dp[j]>=b)///是大于等于{Min = min(Min,dp[j]);}}}printf("%d\n",Min-b);}return 0;
}

转载于:https://www.cnblogs.com/biu-biu-biu-/p/5741313.html

B--Bookshelf 2相关推荐

  1. POJ 3628 Bookshelf 2

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

  2. POJ 3628 Bookshelf 2 (01背包)

    Bookshelf 2 Time Limit: 1000MS   Memory Limit: 65536K Description Farmer John recently bought anothe ...

  3. Bookshelf 2

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

  4. Bookshelf 2 简单DFS

    链接:https://ac.nowcoder.com/acm/contest/993/C 来源:牛客网 题目描述 Farmer John recently bought another bookshe ...

  5. P1848 [USACO12OPEN]Bookshelf G(线段树优化 DP)

    P1848 [USACO12OPEN]Bookshelf G 有nnn间物品,每个物品有两个属性Wi,HiW_i, H_iWi​,Hi​,宽度跟高度,要求把这nnn件物品划分成若干连续的组,每组内∑W ...

  6. 【 POJ - 3628 】Bookshelf 2(dfs 或 dp,0-1背包)

    题干: Farmer John recently bought another bookshelf for the cow library, but the shelf is getting fill ...

  7. 【CodeForces - 294B】Shaass and Bookshelf(枚举,贪心,思维,组内贪心组间dp)

    题干: Shaass has n books. He wants to make a bookshelf for all his books. He wants the bookshelf's dim ...

  8. POJ3628:Bookshelf 2【01背包】

    Description Farmer John recently bought another bookshelf for the cow library, but the shelf is gett ...

  9. POJ 3268 Bookshelf 2 动态规划法题解

    Description Farmer John recently bought another bookshelf for the cow library, but the shelf is gett ...

  10. 书架bookshelf

    第一题 书架 [问题描述] 为了方便同学们查阅资料,程序设计兴趣小组的辅导老师打算将积攒了很多年的n本书放到上课教室的书架上去. 教室的书架是一层一层叠起来的,每一层最多可以放m本书.每一层的高度由放 ...

最新文章

  1. NLP - 15 分钟搭建中文文本分类模型
  2. 寒武纪宣布完成B轮融资 整体估值达25亿美元
  3. hive olap 数据仓库_数据仓库那些事儿
  4. Linux文件类型有哪些?
  5. 内存管理单元--MMU
  6. python基础:细节问题梳理
  7. 卡罗林斯卡学院(Karolinska Institute)
  8. 身边的设计模式(一):单例 与 RedisCacheManager
  9. 防止System.exit调用
  10. 映世便携音箱我对你一见钟情啦~
  11. 3结构介绍_接收机基本结构
  12. 如何在微信小程序中实现与客户实时会话(聊天)
  13. js运算符优先级速查表
  14. 高等数学 —— 二元函数极值存在定理与拉格朗日乘数法求最值
  15. 智能云亮相百度世界2020:重磅发布和升级十大产品,加速AI新基建
  16. 多场景双师课堂解决方案
  17. 百度小程序怎么添加到主屏幕将百度小程序放到手机桌面?
  18. 当别人加快脚步的时候,你更应该慢下来
  19. java超级计算器,jdk自带类
  20. 红帽linux 反编译,RedHat2018_Reverse

热门文章

  1. 游戏基础体验研究:玩家想要什么样的美术品质?
  2. pip更新方法(win与linux)版本
  3. 由于没有远程桌面授权服务器可以提供许可证,远程回话被中断
  4. PHP面试题:简述Linux下安装PHP的过程?
  5. 第一章 SDN介绍 (附件3)【云计算,SDN,虚拟化三者关系】
  6. 零基础学Python-爬虫-2、scrapy框架(测试案例篇·技术点在后面文章内讲解)【测试将一篇小说的所有访问路径与标题存储到一个文件下】
  7. 利用dbms_metadata.get_ddl查看DDL语句(原创)
  8. 简单DP (Preparing for Xtreme 12.0) | STL map使用
  9. HDU 1043 Eight(八数码)
  10. Vim 安装 YouCompleteMe