POJ 3628 Bookshelf 2(01背包)
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 9488 | Accepted: 4311 |
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
一种做法是用dp[i]代表不超过i的可堆到最大程度,然后从m开始寻找第一个大于等于m的dp[i]就是答案,我用的是恰好装满的初始化条件,若可以出现一个恰好装满的解就输出……题目的S小于2000W其实比较大,1000W就差不多了
代码:
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<sstream>
#include<cstring>
#include<bitset>
#include<cstdio>
#include<string>
#include<deque>
#include<stack>
#include<cmath>
#include<queue>
#include<set>
#include<map>
using namespace std;
#define INF 0x3f3f3f3f
#define CLR(x,y) memset(x,y,sizeof(x))
#define LC(x) (x<<1)
#define RC(x) ((x<<1)+1)
#define MID(x,y) ((x+y)>>1)
typedef pair<int,int> pii;
typedef long long LL;
const double PI=acos(-1.0);
const int N=10000010;
int dp[N];
int cow[30];
int n,m;
void zero_one_pack(int c,int w,int V)
{
for (int i=V; i>=c; --i)
if(dp[i-c]+w>dp[i])
dp[i]=dp[i-c]+w;
}
int main(void)
{
int i,ans;
while (~scanf("%d%d",&n,&m))
{
CLR(dp,-INF);
dp[0]=0;
int sum=0;
for (i=0; i<n; ++i)
{
scanf("%d",&cow[i]);
sum+=cow[i];
}
for (i=0; i<n; ++i)
zero_one_pack(cow[i],cow[i],sum);
for (i=m; i<=sum; ++i)
{
if(dp[i]>=0)
{
printf("%d\n",i-m);
break;
}
}
}
return 0;
}
POJ 3628 Bookshelf 2(01背包)的更多相关文章
- POJ 3628 Bookshelf 2 0-1背包
传送门:http://poj.org/problem?id=3628 题目看了老半天,牛来叠罗汉- -|||和书架什么关系啊.. 大意是:一群牛来叠罗汉,求超过书架的最小高度. 0-1背包的问题,对于 ...
- POJ 3628 Bookshelf 2 (01背包)
Bookshelf 2 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7496 Accepted: 3451 Descr ...
- 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 ...
随机推荐
- 【python】choice函数
来源:http://www.runoob.com/python/func-number-choice.html 描述 choice() 方法返回一个列表,元组或字符串的随机项. 语法 以下是 choi ...
- 【好用的小技巧】win8兼容、网页不让复制
1.今天下了个matlab7,我用的是win8系统,不兼容. 解决:鼠标右键matlab7的快捷键,点击属性,选择兼容性,选择window vista即可运行 2.在一个 网页上看到一个对自己很有帮助 ...
- 【2016-08-06】QTableWidget的一些用法总结
1. QTableWidget的列宽如何自适应显示区域大小? QTableWidget的列头继承自QHeaderView,因此如果不使用固定列宽而需要Table中多列的列宽自适应显示区域大小的话, 可 ...
- How to Optimize Battery Health?
1. click on the battery icon from taskbar next to the date and time. 2. click "More power optio ...
- Android之事件分发机制
本文主要包括以下内容 view的事件分发 viewGroup的事件分发 首先来看两张图 在执行touch事件时 首先执行dispatchTouchEvent方法,执行事件分发. 再执行onInterc ...
- linux下常见的文件夹含义
1./bin :获得最小的系统可操作性所需要的命令2./boot :内核和加载内核所需的文件3./dev :终端.磁盘.调制解调器等的设备项4./etc :关键的启动文件和配置文件5./home :用 ...
- Integer取值范围和NumberFormatException的解决
项目有个查询当地新闻的接口,从GEO文件中取得code,后台查询. 下午测试的时候查询日本:3920000000,结果报java.lang.NumberFormatException,数字格式化异常, ...
- lucene 搜索引擎使用案例
1.使用定时框架Quartz.Net创建索引库,引用类库文件有Common.Logging.dll.Lucene.Net.dll,PanGu.dll,PanGu.HighLight.dll,PanGu ...
- RETINA显示屏下ICON优化方法
便于理解,先来了解几个名词: dpi(dots per inch),每英寸的点数,用来测量任何设备的硬件分辨率.一个21”的屏幕可以拥有1680 X 1050 的分辨率,27”的屏幕也可以拥有相同的分 ...
- wp控件
导航控件 Silverlight的Windows Phone应用程序是基于一种可以让用户在不同页面内容间来回导航的页面模型.这个模型是基于其中的frame控件,而页面间的导航就是靠它. 下面的表格列出 ...