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头奶牛,给出其各自身高hi,一书架高B,奶牛们需要叠在一起并达到不小于B的高度,求奶牛总高度与B差值的min值

题解:因为每选择一头牛,由于其身高的不确定性,它站在之前所有牛的背上后的结果有很大可能会影响到最终答案,即选择不同的x头牛高度会影响到之后选择牛的决定,                   令人想到DP(按照一定规律在每一步取最优结果)
又因为每头牛都是特殊的(滑稽),即对于牛i只有两种状态:参与叠罗汉(1),不参与叠罗汉(0)
显然:伟大的0-1背包
f[i][j]表示在前i头牛中总高<=j时这叠牛的高度,则循环维护这个DP数组便可以得到最终答案
状态转移方程很好写: f[i][j]=max(f[i-1][j](不参与),f[i-1][j-1]+hj(参与))
接着我们就需要确定i,j的上下界以便写出程序,i显然:1<=i<=n,那么j呢?从题中我们发现牛的总高需要>=B(书架高度),因此不能用B作为j的上界,那么上界 究竟如何确定呢?
若是能把这段程序打出来,即使空掉上界不写,我们也很容易就可以发现j的上界决定了最多可以与牛的高度比较到哪里!而无疑B最多与所有牛叠在一起的高度比较 (再多就没有牛了),那么上界就可以确定了,
jhi<=j<=sum_cow_height(牛的高度总和)
code:
 #include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<algorithm>
#include<iostream>
using namespace std;
const int maxn=+;
const int maxm=*+;
int n,shelf,total;
int cow[maxn],f[maxm];
bool mmp(int a,int b){return a>b;}
int main()
{ scanf("%d%d",&n,&shelf);
for(int i=;i<=n;i++) scanf("%d",&cow[i]),total+=cow[i];
for(int i=;i<=n;i++)
for(int j=total;j>=cow[i];j--)
f[j]=max(f[j],f[j-cow[i]]+cow[i]);
int i;
for(i=;i<=total;i++)
if(f[i]>=shelf) break;
printf("%d",f[i]-shelf);
return ;
}

 



 

poj_3628 Bookshelf 2的更多相关文章

  1. bookshelf

    nodejs mysql ORM 比node-mysql好用多了. bookshelf 支持restful功能,用到的时候研究下:https://www.sitepoint.com/getting-s ...

  2. POJ3628 Bookshelf 2(01背包+dfs)

    Bookshelf 2 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8745   Accepted: 3974 Descr ...

  3. Bookshelf 2

    Bookshelf 2 Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit  ...

  4. POJ 3628 Bookshelf 2(01背包)

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

  5. POJ3628:Bookshelf 2【01背包】

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

  6. Node的关系型数据库ORM库:bookshelf

    NodeJs 关系数据库ORM库:Bookshelf.js bookshelf.js是基于knex的一个关系型数据库的ORM库.简单易用,内置了Promise的支持.这里主要罗列一些使用的例子,例子就 ...

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

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

  8. HOJ-2056 Bookshelf(线性动态规划)

    L is a rather sluttish guy. He almost never clean up his surroundings or regulate his personal goods ...

  9. 书架 bookshelf

    书架 bookshelf 题目描述 当Farmer John闲下来的时候,他喜欢坐下来读一本好书. 多年来,他已经收集了N本书 (1 <= N <= 100,000). 他想要建立一个多层 ...

随机推荐

  1. 使用EntityFrameworkCore 连接 MySql

    上篇文章介绍了如何在dotnetcore下使用Dapper连接MySql,这里再介绍使用使用EntityFrameworkCore 连接 MySql. 新建控制台项目,安装下面两个nuget包: In ...

  2. 计算机上面常用的计算单位 & 个人计算机架构与接口设备

    不多说,直接上干货! 计算机上面常用的计算单位 容量单位  速度单位  此网络常使用的单位为 Mbps 是 Mbits per second,亦即是每秒多少 Mbit. 个人计算机架构与接口设备 整个 ...

  3. 分析org.rpgpoet.Music.wizards.length

    例子如下: package bazola; public class Gabriel { static int n = org.rpgpoet.Music.wizards.length; } pack ...

  4. Python&Appium实现安卓手机图形解锁

    首先,在解锁状态下,建立一个Session,打开APP.然后,调用press_keycode()方法传入整型数值"26",锁定屏幕.通过implicitly_wait()方法等待两 ...

  5. R语言---- 数据的基本运算

    一.基本运算符号 1.基本数学计算 +.-.*./.^.%%(求模).%/%(整除)注意:求模运算两边若为小数,则整数和小数部分分别求模.例:5.6%%2.2 2.比较运算 >.<.> ...

  6. Tensorflow 方法记录

    1.tf.convert_to_tensor:传入的list必须是一个有固定长度的list,如果为2维的list,第二维的list的长度必须是固定. 2.tf.layers.conv1d(),默认宽卷 ...

  7. docker 创建基础镜像

    docker 安装卸妆官方说明:  https://docs.docker.com/install/linux/docker-ee/centos/#os-requirements 本系列基于cento ...

  8. jenkins-node-pipeline

    Jenkins node创建 1.jenkins搭建参考我的另外一篇文章:    http://www.cnblogs.com/cuishuai/p/7544775.html 2.搭建完成后登录,选择 ...

  9. 使用vue2+Axios+Router 之后的总结以及遇到的一些坑

    构建 vue有自己的脚手架构建工具vue-cli,使用起来非常方便,使用webpack来集成各种开发便捷工具,比如: 代码热更新,修改代码之后网页无刷新改变,对前端开发来说非常的方便 PostCss, ...

  10. lucene源码分析(2)读取过程实例

    1.官方提供的代码demo Analyzer analyzer = new StandardAnalyzer(); // Store the index in memory: Directory di ...