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. js便签笔记(2)——DOM元素的特性(Attribute)和属性(Property)

    1.介绍: 上篇js便签笔记http://www.cnblogs.com/wangfupeng1988/p/3626300.html最后提到了dom元素的Attribute和Property,本文简单 ...

  2. Python的Django框架中forms表单类的使用方法详解

    用户表单是Web端的一项基本功能,大而全的Django框架中自然带有现成的基础form对象,本文就Python的Django框架中forms表单类的使用方法详解. Form表单的功能 自动生成HTML ...

  3. System.Windows.Forms.Timer的简单用法

    Timer就是用来计时操作,如:你想在多少秒之后执行某个动作 Timer showTextBoxTimer = new Timer(); //新建一个Timer对象 showTextBoxTimer. ...

  4. C语言求数组的第二大数

    nt second(int value[],int n) { ]; ]; ; ;i < n;i++) { if(value[i] > first) { second = first; fi ...

  5. LeetCode【第1题】Two Sum

    准备刷一刷LeetCode了. 题目: ''' Given an array of integers, return indices of the two numbers such that they ...

  6. 什么是汉明窗?加Hanmming窗的作用?

    什么是汉明窗?加Hanmming窗的作用? 1.什么是汉明窗? 答:我是做语音识别的,我就从语音的角度跟你说一下吧. 语音信号一般在10ms到30ms之间,我们可以把它看成是平稳的.为了处理语音信号, ...

  7. centos 6.5安装docker

    安装linux,需要系统内核为3.x以上,如果centos版本为7以下,先升级系统内核 1.关闭selinux setenforce 0 sed -i '/^SELINUX=/c\SELINUX=di ...

  8. ASP.NET 之 MVC框架及搭建

    一.MVC简介 MVC:Model-View-Controller(模型-视图-控制器),MVC是一种软件开发架构模式. 1.模型(Model) 模型对象是实现应用程序数据域逻辑的应用程序部件. 通常 ...

  9. ssh连接工具中文乱码问题

    在终端运行: export LC_ALL=zh_CN.GB2312;export LANG=zh_CN.GB2312

  10. 通过set赋值,与select赋值的区别

    ---通过set赋值,与select赋值的区别.declare @a int--set @a=(select count(*) from TblStudent)select @a=count(*) f ...