poj_3628 Bookshelf 2

Posted zi-qilin

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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最多与所有牛叠在一起的高度比较 (再多就没有牛了),那么上界就可以确定了,
jhi<=j<=sum_cow_height(牛的高度总和)
code:

#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<algorithm>
#include<iostream>
using namespace std;
const int maxn=20+10;
const int maxm=1000000*20+100;
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=1;i<=n;i++) scanf("%d",&cow[i]),total+=cow[i];
for(int i=1;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=1;i<=total;i++)
if(f[i]>=shelf) break;
printf("%d",f[i]-shelf);
return 0;
}


 







































以上是关于poj_3628 Bookshelf 2的主要内容,如果未能解决你的问题,请参考以下文章

01++ Bookshelf 2

POJ 3268 Bookshelf 2 动态规划法题解

POJ 3268 Bookshelf 2 动态规划法题解

[最短路树] aw3628. 边的删减(dijkstra算法+最短路树+知识理解+好题+aw周赛002_3)

Bookshelf.js belongsToMany 查询列错误

P3628 [APIO2010]特别行动队