poj_3628 Bookshelf 2

时间:2023-01-05 23:35:31

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&lpar;01背包&plus;dfs&rpar;

    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&colon;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. 使用Volley执行网络数据传输

    首先需要实例化一个RequestQueue RequestQueue queue = Volley.newRequestQueue(this); 然后是根据提供的URL请求字符串响应 String u ...

  2. vim的配置文件及常用的快捷键

    一些最简单的配置,即在.vimrc中可以写入的配置: 首先,说明一点,在.vimrc文件中,可以用“  把一行的配置注销掉. set nocompatible  “关闭 vi 兼容模式:其中 comp ...

  3. Linux学习方法之以始为终—Linux工作分类

    /** ****************************************************************************** * @author    暴走的小 ...

  4. dict和set的使用

    使用dict和set dict Python内置了字典:dict的支持,dict全称dictionary,在其他语言中也称为map,使用键-值(key-value)存储,具有极快的查找速度. 举个例子 ...

  5. 基于C&num;的BarCode 39实现

    一.39条码简介 39码是1974年发展出来的条码系统,是一种可供使用者双向扫瞄的分散式条码,也就是说相临两资料码之间,必须包含一个不具任何意义的空白(或细白,其逻辑值为0),且其具有支援文字的能力, ...

  6. jquery自定义进度条与h5原生进度条

      介绍一款自定义的进度条 <div class="box-nine"> <div class="progress"> <!--一 ...

  7. 列表&lpar;list&rpar;之二 -运用篇 -快速生成规律性列表

    生成列表[1*2,3*4,5*6,7*8,9*10,11*12] 方法一:list1=[]for i in range(1,13,2): list1.append(i*(i+1))print(list ...

  8. android 优化之布局优化

    布局优化的思路很简单,尽量减少布局文件的层级,看过系统源码的都知道,Android view绘制都是逐层绘制的,所以布局的层级少了,decodeview的时候绘制工作自然就少了. 那么如何进行布局的优 ...

  9. Dear ImGUI 使用指南

    文档 1)Dear IMGui 2) 知乎 组件 1) Com 如何设置动态字符串? //char*pTest = "aaaa\0bbbb\0cccc\0dddd\0eeee\0\0&quo ...

  10. 05Hadoop 概论

    Hadoop的思想之源:Google Google搜索引擎,Gmail,安卓,AppspotGoogle Maps,Google earth,Google 学术,Google翻译,Google+,下一 ...