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 ...
随机推荐
- SpringCloud之Ribbon(四)
一:Ribbon是什么? Ribbon是Netflix发布的开源项目,主要功能是提供客户端的软件负载均衡算法,将Netflix的中间层服务连接在一起.Ribbon客户端组件提供一系列完善的配置项如连接 ...
- stars-one原创工具——蓝奏云批量下载工具
一款可以批量下载蓝奏云分享的文件夹下的所有文件 基于HtmlUnit和okhttp开源库,所以打包后的jar包文件有点大 蓝奏云下载地址 github地址 需求 之前找电子书资源的时候,网友分享的蓝奏 ...
- MySQL——DOS命令
翻开之前的笔记发现有这么一篇,于是整理了一下发出来加深记忆并分享交流,欢迎纠错,谢谢!!! 1.启动MySQL服务: net start mysql; 2.停止MySQL服务: net stop my ...
- BigInteger的权限设计
通过储存菜单权限的一个字段(id自定义也是可以的) 1 将选中菜单树的id转换成字符数组的形式, 进行BigInteger对权限进行2的权的和计算 public static BigInteger s ...
- Oracle GoldenGate to Confluent with Kafka Connect
Confluent is a company founded by the team that built Apache Kafka. It builds a platform around Kafk ...
- 【Oracle】内连接、外连接、(+)的使用
表各有A, B两列 A B 001 10A 002 20A A B 001 10B 003 30B A B 001 10C 004 40C 连接分为两种:内连接与外连接. A.内连接 内连接,即最常见 ...
- 20191211 HNOI2017模拟赛 C题
题目: 分析: 开始觉得是神仙题... 然后发现n最多有2个质因子 这说明sm呢... 学过物理的小朋友们知道,当一个物体受多个不同方向相同的力时,只有相邻力的夹角相等,受力就会平衡 于是拆扇叶相当于 ...
- 链接拼接的方法(用于解决同一个脚本返回两种不同的url链接的问题)
实例一: 上图所示 爬虫返回的链接有一部分带有http前缀,有一部分没有,且也不知道具体哪些链接会出现没有前缀的情况 后面如果通过返回链接进行再次访问,那么肯定会出现报错的问题 思路: 判断 返回值内 ...
- 冬日曙光——回溯CNN的诞生
前言 卷积神经网络(CNN)作为深度学习的重要一支,在当前计算机视觉领域应用相当广泛.本文回顾了深度学习的发展历程,讲述CNN基本的理论概念和第一代卷积神经网络LeNet-5的建立.文章言有不当之处, ...
- alert弹出窗口,点击确认后关闭页面
alert("点击确认后,关闭页面"); window.opener=null;window.top.open('','_self','');window.close(this);