POJ 3628 Bookshelf 2 (01背包)
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 ≤ 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
Source
题意:就是给出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背包)的更多相关文章
- POJ 3628 Bookshelf 2 0-1背包
传送门:http://poj.org/problem?id=3628 题目看了老半天,牛来叠罗汉- -|||和书架什么关系啊.. 大意是:一群牛来叠罗汉,求超过书架的最小高度. 0-1背包的问题,对于 ...
- POJ 3628 Bookshelf2(0-1背包)
http://poj.org/problem?id=3628 题意:给出一个高度H和n个牛的高度,要求把牛堆叠起来达到H,求出该高度和H的最小差. 思路:首先我们计算出牛的总高度sum,sum-H就相 ...
- POJ 3628 Bookshelf 2【背包型DFS/选or不选】
Bookshelf 2 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11105 Accepted: 4928 Desc ...
- POJ 3211 Washing Clothes(01背包)
POJ 3211 Washing Clothes(01背包) http://poj.org/problem?id=3211 题意: 有m (1~10)种不同颜色的衣服总共n (1~100)件.Dear ...
- POJ 3628 Bookshelf 2【01背包】
题意:给出n头牛的身高,以及一个书架的高度,问怎样选取牛,使得它们的高的和超过书架的高度最小. 将背包容量转化为所有牛的身高之和,就可以用01背包来做=== #include<iostream& ...
- poj 3628 Bookshelf 2
http://poj.org/problem?id=3628 01背包 #include <cstdio> #include <iostream> #include <c ...
- POJ3628 Bookshelf 2(01背包+dfs)
Bookshelf 2 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8745 Accepted: 3974 Descr ...
- [POJ 2184]--Cow Exhibition(0-1背包变形)
题目链接:http://poj.org/problem?id=2184 Cow Exhibition Time Limit: 1000MS Memory Limit: 65536K Total S ...
- POJ 3624 Charm Bracelet(01背包裸题)
Charm Bracelet Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 38909 Accepted: 16862 ...
随机推荐
- NYOJ_75 日期计算 (推断这一天是这一年中的第几天)
题目地址 如题,输入一个日期,格式如:2010 10 24 ,推断这一天是这一年中的第几天. 分析: 官方给的最优答案用了for 和switch语句结合,十分巧妙. 代码 /* 如题,输入一个日期 ...
- wordpress+wampserver
听说过wordpress和joomla这样的简单建站的工具,尽管是PHP,可是看过同事搭建的公司站点.效果真心不错.于是手痒痒尝试一下.由于是搭着玩儿.所以用wordpress+wampserver( ...
- Android手机间使用socket进行文件互传实例
这是一个Android手机间文件传输的例子,两个手机同时装上此app,然后输入接收端的ip,选择文件,可以多选,点确定,就发送到另一个手机,一个简单快捷文件快传实例.可以直接运用到项目中. 下面是文件 ...
- C# 性能优化
StringBuilder sb = new StringBuilder( 256 ). 避免不必要的调用 ToUpper 或 ToLower 方法,可以用Compare忽略大小写比较. 尽量在循环中 ...
- 关于使用toFixed()函数时报错"toFixed() is not a function"的问题
toFixed()函数只有数字类型的参数才可使用,字符串类型的参数需用parseFloat或者parseInt转换后再使用
- Linux 网卡驱动学习(九)(层二转发)
1.mac 地址表的自学习过程 port1上的A计算机要与port2上的B计算机通信时,A发到交换机上,交换机收到信息后,交换机先记录发port1所相应的a的mac地址并记录在自己的mac表中,然后再 ...
- Express框架是什么
Express框架是什么 一.总结 1.express框架:基于node.js的web应用框架,可快速搭建一个完整功能的网站,丰富的HTTP工具以及来自Connect框架的中间件随取随用. 二.Exp ...
- setAttribute的浏览器兼容性
1.element要用getElementById 或者是ByTagName来得到 2.setAttribute("class", vName)中class是指改变"cl ...
- Vue 自定义全局消息框组件
消息弹框组件,默认3秒后自动关闭,可设置info/success/warning/error类型 效果图: 文件目录: Message.vue <template> <transit ...
- CSS外边距合并(塌陷/margin越界)
原文 简书原文:https://www.jianshu.com/p/5f18f12cd162 大纲 1.什么是外边距合并?(折叠外边距) 2.外边距带来的影响 3.折叠的结果 4.产生折叠的原因 5. ...