POJ3628:Bookshelf 2【01背包】

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

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 ≤ BS, 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
简单的 0,1背包问题
01背包(ZeroOnePack): 有N件物品和一个容量为V的背包。(每种物品均只有一件)第i件物品的费用是c[i],价值是w[i]。求解将哪些物品装入背包可使价值总和最大。 01背包(ZeroOnePack): 有N件物品和一个容量为V的背包。(每种物品均只有一件)第i件物品的费用是c[i],价值是w[i]。求解将哪些物品装入背包可使价值总和最大。 这是最基础的背包问题,特点是:每种物品仅有一件,可以选择放或不放。 用子问题定义状态:即f[i][v]表示前i件物品恰放入一个容量为v的背包可以获得的最大价值。则其状态转移方程便是:

f[i][v]=max{f[i-1][v],f[i-1][v-c[i]]+w[i]}

把这个过程理解下:在前i件物品放进容量v的背包时, 它有两种情况: 第一种是第i件不放进去,这时所得价值为:f[i-1][v] 第二种是第i件放进去,这时所得价值为:f[i-1][v-c[i]]+w[i] (第二种是什么意思?就是如果第i件放进去,那么在容量v-c[i]里就要放进前i-1件物品) 最后比较第一种与第二种所得价值的大小,哪种相对大,f[i][v]的值就是哪种。 (这是基础,要理解!) 这里是用二位数组存储的,可以把空间优化,用一位数组存储。 用f[0..v]表示,f[v]表示把前i件物品放入容量为v的背包里得到的价值。把i从1~n(n件)循环后,最后f[v]表示所求最大值。 *这里f[v]就相当于二位数组的f[i][v]。那么,如何得到f[i-1][v]和f[i-1][v-c[i]]+w[i]?(重点!思考)
首先要知道,我们是通过i从1到n的循环来依次表示前i件物品存入的状态。即:for i=1..N
现在思考如何能在是f[v]表示当前状态是容量为v的背包所得价值,而又使f[v]和f[v-c[i]]+w[i]标签前一状态的价值?

逆序!

这就是关键!
1
2
3
for i=1..N
   for v=V..0
        f[v]=max{f[v],f[v-c[i]]+w[i]};
分析上面的代码:当内循环是逆序时,就可以保证后一个f[v]和f[v-c[i]]+w[i]是前一状态的!
此题的代码
#include <stdio.h>
#include <string.h> int dp[1000005],a[10005]; int max(int a,int b)
{
return a>b?a:b;
} int main()
{
int n,m;
while(~scanf("%d%d",&n,&m))
{
int i,j,sum = 0;
memset(dp,0,sizeof(dp));
memset(a,0,sizeof(a));
for(i = 1; i<=n; i++)
{
scanf("%d",&a[i]);
sum+=a[i];
}
for(i = 1; i<=n; i++)
{
for(j = sum; j>=a[i]; j--)
dp[j] = max(dp[j],dp[j-a[i]]+a[i]);
}
for(i = 1; i<=sum; i++)
{
if(dp[i]>=m)
{
printf("%d\n",dp[i]-m);
break;
}
}
} return 0;
}

POJ3628:Bookshelf 2【01背包】的更多相关文章

  1. POJ3628 Bookshelf 2&lpar;01背包&plus;dfs&rpar;

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

  2. POJ 3628 Bookshelf 2 0-1背包

    传送门:http://poj.org/problem?id=3628 题目看了老半天,牛来叠罗汉- -|||和书架什么关系啊.. 大意是:一群牛来叠罗汉,求超过书架的最小高度. 0-1背包的问题,对于 ...

  3. POJ 3628 Bookshelf 2 &lpar;01背包&rpar;

    Bookshelf 2 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7496   Accepted: 3451 Descr ...

  4. Bookshelf 2 01背包

    B - Bookshelf 2 Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu Submi ...

  5. Bookshelf 2&lpar;poj3628&comma;01背包&comma;dp递推&rpar;

    题目链接:Bookshelf 2(点击进入) 题目解读: 给n头牛,给出每个牛的高度h[i],给出一个书架的高度b(所有牛的高度相加>书架高度b),现在把一些牛叠起来(每头牛只能用一次,但不同的 ...

  6. POJ 3628 Bookshelf 2【01背包】

    题意:给出n头牛的身高,以及一个书架的高度,问怎样选取牛,使得它们的高的和超过书架的高度最小. 将背包容量转化为所有牛的身高之和,就可以用01背包来做=== #include<iostream& ...

  7. PKU--3628 Bookshelf 2(01背包)

    题目http://poj.org/problem?id=3628 分析:给定一堆牛的高度,把牛叠加起来的高度超过牛棚的高度. 且是牛叠加的高度与牛棚高度之差最小. 把牛叠加的高度看作是背包的容量,利用 ...

  8. poj 01背包

    首先我是按这篇文章来确定题目的. poj3624 Charm Bracelet 模板题 没有要求填满,所以初始化为0就行 #include<cstdio> #include<algo ...

  9. POJ之01背包系列

    poj3624 Charm Bracelet 模板题 没有要求填满,所以初始化为0就行 #include<cstdio> #include<iostream> using na ...

随机推荐

  1. Unity3D 原生Android结合UnityPlayerActivity开发遇到的问题

    需求是原生Android 的Activity启动UnityPlayerActivity,按Back键后返回原来的Activity 1.在AndroidManifest.xml中的UnityPlayer ...

  2. PHP生成唯一会员卡号

    我们将0-Z(0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ)分别代表数值0-35,如字母Z代表35.这样的话我要得到一个5位的编号,最大信息量就是36的5次方了,36^5 ...

  3. addEventListener、attachEvent、cancelBubble兼容性随笔

    一.前言 1. element.addEventListener(eventType, handler, capture); (1)参数eventType是要注册句柄的事件类型名. (2)参数hand ...

  4. java 四种内部类和内部接口

    /** * 常规内部类:常规内部类没有用static修饰且定义在在外部类类体中. * 1.常规内部类中的方法可以直接使用外部类的实例变量和实例方法. * 2.在常规内部类中可以直接用内部类创建对象 * ...

  5. bzoj 1096&colon; &lbrack;ZJOI2007&rsqb;仓库建设 斜率優化

    1096: [ZJOI2007]仓库建设 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 2242  Solved: 925[Submit][Statu ...

  6. PHP环境搭配

    电脑上如果有apache,必须先卸载了先,如果有集成的环境,类似于apmserver,也必须先停止先.不然安装的时候,会出现修复和卸载选项,而不是典型安装跟用户自定义安装. apache安装目录 E: ...

  7. uva 10161 Ant on a Chessboard 蛇形矩阵 简单数学题

    题目给出如下表的一个矩阵: (红字表示行数或列数) 25 24 23 22 21 5 10 11 12 13 20 9 8 7 14 19 3 2 3 6 15 18 2 1 4 5 16 17 1 ...

  8. 笔记整理--C语言

    linux下错误的捕获:errno和strerror的使用 - Google Chrome (2014/2/26 17:31:39) linux下错误的捕获:errno和strerror的使用 201 ...

  9. smartsvn学习(二)如何在Xcode下使用SVN

    1.Xcode4中苹果有自带的SVN软件------>Organizer------>Repositories   2.SVN checkout到本地后,删除本地file,对服务器有影响吗 ...

  10. WPF 进程间传递参数

    WPF 进程间传递参数          在软件开发中有时需要在一个软件中启动另一个软件,这时用Process.Start(“软件路径”)可以启动另一个软件.如果在这个过程中还需要传递一些参数给新启动 ...