POJ 3628 Bookshelf 2【背包型DFS/选or不选】
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 11105 | Accepted: 4928 |
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
#include<stdio.h>
int cow[];
int b;
int n;
int ans=;
void DFS(int num,int sum)
{
if(sum>=ans) return;
if(num==n)
{
if(sum>=b) ans=sum;
return;
}
DFS(num+,sum);
DFS(num+,sum+cow[num]);
}
int main()
{
//freopen("input","r",stdin);
int i,j,cnt,tmp;
int sum;
scanf("%d %d",&n,&b);
for(i=;i<n;i++)
scanf("%d",&cow[i]);
DFS(,);
printf("%d",ans-b);
return ;
}
POJ 3628 Bookshelf 2【背包型DFS/选or不选】的更多相关文章
- 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: 9488 Accepted: 4311 Descr ...
- POJ 3628 Bookshelf 2【01背包】
题意:给出n头牛的身高,以及一个书架的高度,问怎样选取牛,使得它们的高的和超过书架的高度最小. 将背包容量转化为所有牛的身高之和,就可以用01背包来做=== #include<iostream& ...
- POJ 3628 Bookshelf 2 (01背包)
Bookshelf 2 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7496 Accepted: 3451 Descr ...
- 洛谷 P1036 选数【背包型DFS/选or不选】
题目描述 已知 n 个整数 x1,x2,…,xn,以及一个整数 k(k<n).从 n 个整数中任选 k 个整数相加,可分别得到一系列的和.例如当 n=4,k=3,4 个整数分别为 3,7,12, ...
- nyoj zb的生日【背包型DFS/选or不选】
zb的生日 时间限制:3000 ms | 内存限制:65535 KB 难度:2 描述 今天是阴历七月初五,acm队员zb的生日.zb正在和C小加.never在武汉集训.他想给这两位兄弟买点什么 ...
- poj 3628 Bookshelf 2 基本01背包
题目大意:FJ有n头奶牛,和一个高为h的架子,给出每头奶牛高度,求使奶牛叠加起来超过架子的最低高度是多少. 题目思路:求出奶牛叠加能达到的所有高度,并用dp[]保存,最后进行遍历,找出与h差最小的dp ...
- poj 3628 Bookshelf 2
http://poj.org/problem?id=3628 01背包 #include <cstdio> #include <iostream> #include <c ...
- POJ 3628 Bookshelf 2 题解
本题解法非常多,由于给出的数据特殊性故此能够使用DFS和BFS,也能够使用01背包DP思想来解. 由于一般大家都使用DFS,这里使用非常少人使用的BFS.缺点是比DFS更加耗内存,只是长处是速度比DF ...
随机推荐
- 【JZOJ3317】管道
description Hotham市再次被Jester袭击.这一次,Jester攻击的目标是Hotham的供水系统.Hotham的淡水存储在N个水库,由M个管道连接. 任意2个水库之间至少有一条路径 ...
- 数论整除——cf1059D
用map是卡着过去的..题解用vector+离散化后常数小了十倍.. 总之就是把所有模数给保存下来然后离散化,再去匹配一下即可,最后有个细节 自己的 #include<bits/stdc++.h ...
- coreseek 基与Sphinx 的全文索引
假设有两张那个表,分别为articles,article_photos两张表.搜索的时候,要匹配articles.title.articles.intro.article_photos.caption ...
- hibernate查询timestamp条件
参考https://blog.csdn.net/zuozuoshenghen/article/details/50540661 Mysql中Timestamp字段的格式为yyyy-MM-dd HH-m ...
- 利用refind实现UEFI多系统引导
使用DiskGenius Pro给ESP分区指定盘符,目的是为了让ESP分区在硬盘上可见 使用BOOTICE工具中的UEFI选项卡中的功能调整引导顺序 修改启动序列-->EFI NetWork- ...
- <每日一题>题目6:二分查找
#二分查找 ''' 1.end问题 2.44对应的end<start 找不到情况 3.返回值递归的情况 4,611,aim太大的情况 ''' l = [2,3,5,10,15,16,18,22, ...
- neo4j 实战、实例、示例 创建电影关系图 -1
1. 创建关系 因为代码占篇幅太大,创建整个"电源关系图"的代码在文章最下方. 2. 简单分析创建语句 2.1 创建电影节点 CREATE (TheMatrix:Movie {ti ...
- jQuery实现textarea高度根据内容自适应
//jQuery实现textarea高度根据内容自适应 $.fn.extend({ txtaAutoHeight: function () { return this.each(function () ...
- Apache 禁用IP 访问 和 HTTP 跳转 HTTPS
如果需要 禁用IP 访问并且 需要 HTTP 跳转到 HTTPS <VirtualHost *:80> ServerName xxx.xxx.com RewriteEngine On Re ...
- win10 安装face_recongnition
1.安装dlib https://stackoverflow.com/questions/41912372/dlib-installation-on-windows-10 2.安装face_recon ...