Bookshelf 2
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
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std ;
int dp[] ;
int B , n ;
int a[] ;
int cmp (int x , int y ) {
return x > y ;
}
int main () {
// freopen ("a.txt" , "r" , stdin );
while (scanf ("%d%d" , &n , &B ) != EOF ) {
for (int i = ; i < n ; i++ ) {
scanf ("%d" , &a[i] ) ;
}
sort ( a , a + n , cmp ) ;
for (int i = ; i <= B + ; i++ ) {
dp[i] = ;
}
dp[] = ;
for (int i = ; i < n ; i++ ) {
for (int j = B + ; j >= a[i] ; j-- ) {
if ( dp[j - a[i]] ) {
dp[j] = ;
// printf("%d " , j) ;
}
}
// printf ("\n") ;
}
// printf("\n") ;
for (int j = B ; j <= B + ; j++ ) {
if ( dp[j] ) {
printf ("%d\n" , j - B ) ;
break ;
}
}
}
return ;
}
01背包
Bookshelf 2的更多相关文章
- bookshelf
nodejs mysql ORM 比node-mysql好用多了. bookshelf 支持restful功能,用到的时候研究下:https://www.sitepoint.com/getting-s ...
- POJ3628 Bookshelf 2(01背包+dfs)
Bookshelf 2 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8745 Accepted: 3974 Descr ...
- POJ 3628 Bookshelf 2(01背包)
Bookshelf 2 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9488 Accepted: 4311 Descr ...
- POJ3628:Bookshelf 2【01背包】
Description Farmer John recently bought another bookshelf for the cow library, but the shelf is gett ...
- Node的关系型数据库ORM库:bookshelf
NodeJs 关系数据库ORM库:Bookshelf.js bookshelf.js是基于knex的一个关系型数据库的ORM库.简单易用,内置了Promise的支持.这里主要罗列一些使用的例子,例子就 ...
- POJ 3268 Bookshelf 2 动态规划法题解
Description Farmer John recently bought another bookshelf for the cow library, but the shelf is gett ...
- HOJ-2056 Bookshelf(线性动态规划)
L is a rather sluttish guy. He almost never clean up his surroundings or regulate his personal goods ...
- poj_3628 Bookshelf 2
Description Farmer John recently bought another bookshelf for the cow library, but the shelf is gett ...
- 书架 bookshelf
书架 bookshelf 题目描述 当Farmer John闲下来的时候,他喜欢坐下来读一本好书. 多年来,他已经收集了N本书 (1 <= N <= 100,000). 他想要建立一个多层 ...
随机推荐
- 恢复windows 的快捷方式打开方法,亲测1-7恢复,
相信有些用户曾试过错误地把LNK文件的打开方式更改其他文件,导致系统所有的快捷方式都失效.在vista与Windows7系统还不普遍使用的时候,相信大家会有点惊慌失措,不要紧,下面只要大家进行如下操作 ...
- Ado.net 通用访问类
public class DbHelperSQL { private static string connString = ConfigurationManager.ConnectionStrings ...
- xcode更新,想想也是醉了
每次更新,都要整个文件全部更新,这下载速度,想想也是醉了,苹果你就不能搞了更新包吗!!
- js判断页面出现滚动条
当可视区域小于页面的实际高度时,判定为出现滚动条 当我们在获取页面的offsetHeight高度时是包括了浏览器的边框的,浏览器的边框是2个像素, if (document.documentEleme ...
- Jquery实现异步上传图片
利用jQuery的ajax函数就可以实现异步上传图片了.一开始我是想在处理程序中,直接用context.Request.Files来获取页面中的input file,但是不知道为什么一次获取不了.网上 ...
- datepicker自定义 -- iOS
/** * 创建时间选择器 */ - (void)createPickerView { self.datePicker = [[UIDatePicker alloc] init]; _datePick ...
- 页面无法访问 css文件加载问题
1.青奥项目的web配置: 后缀为.html和.vm的请求会被控制器拦截. 控制器没设置目标资源,所以无法访问到资源! 2.文件不能放在vm文件夹下,因为设置了视图解析,vm文件夹下的文件只有后缀为 ...
- .Net身份验证概述
一直以来,所有的系统基本都会有用户的登陆验证过程,整个过程其实也不难理解,就是对于cookie的解析.微软的.Net平台围绕用户身份验证授权也有好几个版本了,从早期的Membership到Identi ...
- 缓存插件 EHCache
EHCache是来自sourceforge(http://ehcache.sourceforge.net/)的开源项目,也是纯Java实现的简单.快速的Cache组件. 下载jar包 Ehcache ...
- Vijos1901 学姐的钱包
描述 学姐每次出门逛街都要带恰好M元钱, 不过她今天却忘记带钱包了.可怜的doc只好自己凑钱给学姐, 但是他口袋里只有一元钱.好在doc的N位朋友们都特别有钱, 他们答应与doc作一些交换.其中第i位 ...