Bookshelf 2 01背包
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 头牛,书架高度M, 列举每头牛的高度, 求牛叠加起来的超过书架的最小高度。 题目思路:求出奶牛叠加能达到的所有高度,并用dp[]保存,最后进行遍历,找出与h差最小的dp[]即所求答案。 代码:
#include<stdio.h>
#include<string.h>
#define max(a, b)(a > b ? a : b)
#define N 1000010
int main(void)
{
int dp[N];
int i, j, n, m;
int w[N];
while(scanf("%d%d", &n, &m) != EOF)
{
int sum = 0;//所有牛加起来的总高度。
memset(dp, 0, sizeof(dp));
for(i = 1; i <= n; i++)
{
scanf("%d", &w[i]);
sum += w[i];
}
int ans = 0;
for(i = 1; i <= n; i++)
{
for(j = sum; j >= w[i]; j--)//j要倒推才能保证在推dp[j]时, max里dp[j]和dp[j-w[i]]保存的是状态dp[i-1][j] 和dp[i-1][j-w[i]]的值。
{
dp[j] = max(dp[j], dp[j-w[i]] + w[i]);
}
}
for(i = 1; i <= sum; i++)//遍历dp数组找到超过书架的最小高度直接保存跳出循环。
{
if(dp[i] >= m)
{
ans = dp[i];
break;
}
}
printf("%d\n", ans - m);
}
return 0;
}
Bookshelf 2 01背包的更多相关文章
- POJ3628 Bookshelf 2(01背包+dfs)
Bookshelf 2 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8745 Accepted: 3974 Descr ...
- 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 ...
- POJ3628:Bookshelf 2【01背包】
Description Farmer John recently bought another bookshelf for the cow library, but the shelf is gett ...
- Bookshelf 2(poj3628,01背包,dp递推)
题目链接:Bookshelf 2(点击进入) 题目解读: 给n头牛,给出每个牛的高度h[i],给出一个书架的高度b(所有牛的高度相加>书架高度b),现在把一些牛叠起来(每头牛只能用一次,但不同的 ...
- POJ 3628 Bookshelf 2【01背包】
题意:给出n头牛的身高,以及一个书架的高度,问怎样选取牛,使得它们的高的和超过书架的高度最小. 将背包容量转化为所有牛的身高之和,就可以用01背包来做=== #include<iostream& ...
- PKU--3628 Bookshelf 2(01背包)
题目http://poj.org/problem?id=3628 分析:给定一堆牛的高度,把牛叠加起来的高度超过牛棚的高度. 且是牛叠加的高度与牛棚高度之差最小. 把牛叠加的高度看作是背包的容量,利用 ...
- UVALive 4870 Roller Coaster --01背包
题意:过山车有n个区域,一个人有两个值F,D,在每个区域有两种选择: 1.睁眼: F += f[i], D += d[i] 2.闭眼: F = F , D -= K 问在D小于等于一定限度的时 ...
- POJ1112 Team Them Up![二分图染色 补图 01背包]
Team Them Up! Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 7608 Accepted: 2041 S ...
随机推荐
- 【5min+】传说中的孪生兄弟? Memory and Span
系列介绍 [五分钟的dotnet]是一个利用您的碎片化时间来学习和丰富.net知识的博文系列.它所包含了.net体系中可能会涉及到的方方面面,比如C#的小细节,AspnetCore,微服务中的.net ...
- 如何通过Java8的方式去统计程序执行时间?
代码如下所示 import java.time.Duration; import java.time.Instant; import java.util.concurrent.TimeUnit; pu ...
- python面向对象(一切皆对象)
使用面向对象的思想设计一个乌龟的角色: 表面特征:绿色.有4条腿.重10kg.有外壳等等 行为特征:爬.吃.睡觉.将头和四肢缩到壳里等等 class tortoise: bodycolor = &qu ...
- python类型-序列
注:本文档主要是学习<Python核心编程(第二版)>时做的资料整理. 1.序列 序列的成员是有序排列的,并且可以通过下标偏移量访问到它的一个或者几个成员,包括字符串(普通字符串和Unic ...
- 《Sequence Models》课堂笔记
Lesson 5 Sequence Models 这篇文章其实是 Coursera 上吴恩达老师的深度学习专业课程的第五门课程的课程笔记. 参考了其他人的笔记继续归纳的. 符号定义 假如我们想要建立一 ...
- 用Python实现根据角4点进行矩阵二维插值并画出伪彩色图
哈哈,题目取得这么绕,其实就是自己写了一个很渣的类似图像放大的算法.已知矩阵四周的4点,扩展成更大的矩阵,中间的元素值均匀插入,例如: 矩阵: 1 2 3 4 扩展成3x3的: 1 1.5 2 ...
- RTMP、HTTP、HLS协议比较
RTMP HLS HTTP 直播协议一次看个够 直播从2016年一路火到了2017年,如今要在自己的App里加入直播功能,只要找一个现成的SDK就行了,什么拍摄.美颜.推流,一条龙服务.不过作为直播身 ...
- windows丢失文件的恢复技巧
这几天在使用STVD调试程序的时候,突然跳出来一个“共享冲突”错误,当时并没有在意,点确定后赶紧CTRL+S,然后就一直死在那里了... 结束任务,重启STVD,提示找不到main.c,到此也不以为然 ...
- Jconsole或者VisualVM监控远程主机(阿里云,jdk11或者8)
准备: 1 一个war包或者jar包,这里我用springboot的 2 linux环境,安装tomcat,jdk,我用的jdk11和tomcat9,jdk11和8的拷贝权限文件路径有点不一样,这个需 ...
- Java后端开发工程师是否该转大数据开发?
撰写我对java后端开发工程师选择方向的想法,写给在java后端选择转方向的人 背景 看到一些java开发工程师,对java后端薪酬太悲观了.认为换去大数据领域就会高工资.觉得java后端没有前途.我 ...