poj_3628 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 题意:有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最多与所有牛叠在一起的高度比较 (再多就没有牛了),那么上界就可以确定了,
而j:hi<=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的更多相关文章
- 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 ...
- Bookshelf 2
Bookshelf 2 Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit ...
- 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 ...
- 书架 bookshelf
书架 bookshelf 题目描述 当Farmer John闲下来的时候,他喜欢坐下来读一本好书. 多年来,他已经收集了N本书 (1 <= N <= 100,000). 他想要建立一个多层 ...
随机推荐
- 【数组】Majority Element II
题目: Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The alg ...
- jetbrains golang IDE
非常好的IDE,叫goland. 支持最新的golang1.8了 下载地址: https://www.jetbrains.com/go/ 开始使用手册: https://www.jetbrains.c ...
- windows下安装并使用redis
一.安装前首先了解一下phpinfo里面的一些信息,能否正确安装非常有帮助. (下图是我的本机环境) compiler :编译器 Architecture :CPU架构 Configuration F ...
- freepbx13通话无声音通话自动挂断
在阿里云上用脚本一键搭建好了freepbx13,但是在创建好sip分机之后,打电话没有声音,双方都听不到对方的声音.这个是nat问题. 这里有个坑我提醒下大家,就是我们最好不要用台式电脑进行测试通话. ...
- linux环境下编译C++ 程序
GCC(GNU Compiler Collection)是Linux下最主要的编译工具,GCC不仅功能非常强大,结构也异常灵活.它可以通过不同的前端模块来支持各种语言,如:Java.Fortran.P ...
- SpringCloud源码解读
http://blog.didispace.com/Spring-Cloud%E5%9F%BA%E7%A1%80%E6%95%99%E7%A8%8B/
- vue 2.x之组件的数据传递(一)
这是根据官方提供的脚手架vue-cli搭建,通过简单的案例来介绍vue数据的传递的方式,根据自己平时用到的,来做简单的总结: 1.父组件传递数据给子组件 父组件传递数据给子组件,需要把子组件引入,并挂 ...
- Border属性的各种变化
本文前部分转自http://www.cnblogs.com/binyong/archive/2009/02/21/1395386.html,但是文章并未解释实现的原理,因此,后面本文也对次进行了解释. ...
- 关于Win8快速启动失效解决
注册表修改HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager 下BootExecute值为 autocheck空格 ...
- XP系统运行wpf程序出现透明现象的解决
xp 虚拟机运行WPF程序的时候,有部分控件透明,在这儿有一个可能的解决方案: 关闭系统的硬件加速: 1. 按“WIN” + R 键,在“运行”输入框中输入“dxdiag”: 2. 在“DirectX ...