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

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

Source

USACO 2007 December Bronze

题意:就是给出n和b。然后给出n个数。用这n个数中的某些。求出一个和,这个和是>=b的最小值,输出最小值与b的差。

分析:这道题非常easy。是非常明显的01背包问题,这里的n个物品,每一个物品的重量为c[i],价值为w[i]而且c[i]==w[i],。容量为全部c[i]的和sum。仅仅要在f[]中从头開始找。找到一个最小>=b的就是题目要的解

一開始看错题了,以为是最接近b的值。WA无数次。

代码:46MS

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
using namespace std;
#define M 1000005
#define N 10005
int map[N],dp[M];
int main()
{
int i,j,n,v,sum;
while(cin>>n>>v)
{
memset(dp,0,sizeof(dp));
memset(map,0,sizeof(map));
sum=0;
for(i=1;i<=n;i++) {cin>>map[i];sum+=map[i];}
for(i=1;i<=n;i++)
for(j=sum;j>=map[i];j--)
dp[j]=max(dp[j],dp[j-map[i]]+map[i]);
for(i=1;i<=sum;i++)
{
if(dp[i]>=v) //明显。第一个比v大的一定满足条件。
{cout<<dp[i]-v<<endl;break;}
}
}
return 0;
}

POJ 3628 Bookshelf 2 (01背包)的更多相关文章

  1. POJ 3628 Bookshelf 2 0-1背包

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

  2. POJ 3628 Bookshelf2(0-1背包)

    http://poj.org/problem?id=3628 题意:给出一个高度H和n个牛的高度,要求把牛堆叠起来达到H,求出该高度和H的最小差. 思路:首先我们计算出牛的总高度sum,sum-H就相 ...

  3. POJ 3628 Bookshelf 2【背包型DFS/选or不选】

    Bookshelf 2 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11105   Accepted: 4928 Desc ...

  4. POJ 3211 Washing Clothes(01背包)

    POJ 3211 Washing Clothes(01背包) http://poj.org/problem?id=3211 题意: 有m (1~10)种不同颜色的衣服总共n (1~100)件.Dear ...

  5. POJ 3628 Bookshelf 2【01背包】

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

  6. poj 3628 Bookshelf 2

    http://poj.org/problem?id=3628 01背包 #include <cstdio> #include <iostream> #include <c ...

  7. POJ3628 Bookshelf 2(01背包+dfs)

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

  8. [POJ 2184]--Cow Exhibition(0-1背包变形)

    题目链接:http://poj.org/problem?id=2184 Cow Exhibition Time Limit: 1000MS   Memory Limit: 65536K Total S ...

  9. POJ 3624 Charm Bracelet(01背包裸题)

    Charm Bracelet Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 38909   Accepted: 16862 ...

随机推荐

  1. js02 变量数据类型

    变量 JavaScript 是一种弱类型的脚本语言 var c = 3:即变量的声明(变量使用之前必须加var声明,编程规范) 变量的命名规则! 1.变量命名必须以字母或是下标符号”_”或者”$”为开 ...

  2. 6lession-基本数据类型

    因为自己是根据网上教程学习的,所以以下内容参考自 http://www.w3cschool.cc/python/python-variable-types.html python支持物种数据类型,分别 ...

  3. WebService三大基本元素 SOAP WSDL UDDI

    转自:https://blog.csdn.net/hhooong/article/details/51763128 1.SOAP 即 Simple Object AccessProtocol 也就是简 ...

  4. VNC Server模拟攻击实战

    VNC目前已被广泛应用的一个远程控制程序,很多攻击者对VNC的攻击技术研究热情是高涨的,丝毫不亚于对Windows的远程桌面(3389).PcAnywhere的攻击研究.从最开始爆发出来的VNC的低版 ...

  5. gomail发送附件

    采用github.com/go-gomail/gomail/ 的邮件功能,可以发送附件 以及html文档,下面是其给出的demo,测试通过. package main //cmd: go get go ...

  6. echarts tooltip提示框 自定义小圆点(颜色、形状和大小等等)

    项目是拿 echarts + 百度地图 来做可视化界面,现在到收尾阶段慢慢优化. 先附代码: formatter: function(params) { var result = '' params. ...

  7. C语言之函数指针、回调函数的使用

    一.背景 首先看下如下代码,这个定义是放在头文件的,在程序中tCdrvCallbackFkt也定义了另一个变量,而且括号后面还跟定义了几个变量,不理解这个定义. typedef void (PUBLI ...

  8. Method of address space layout randomization for windows operating systems

    A system and method for address space layout randomization ("ASLR") for a Windows operatin ...

  9. C++ Traits 技术

    Tarits.特性的复数. c++萃取技术就是指它. 实现方式是模板特化. STL中涉及到iterator的地方常常能用到它. gcc的STL与VS的STL略有区别. vs中下列代码,把鼠标放在ite ...

  10. 发送 email 过程

    发送 email 过程 SMTP基本命令集: 命令 描述 ---------- HELO 向服务器标识用户身份发送者能欺骗,说谎,但一般情况下服务器都能检测到. MAIL 初始化邮件传输 mail f ...