链接:https://ac.nowcoder.com/acm/contest/993/C
来源:牛客网

题目描述

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.

输入描述:

* Line 1: Two space-separated integers: N and B
* Lines 2..N+1: Line i+1 contains a single integer: Hi

输出描述:

* 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.
示例1

输入

复制

5 16
3
1
3
5
6

输出

复制

1

说明

Here we use cows 1, 3, 4, and 5, for a total height of 3 + 3 + 5 + 6 = 17.
It is not possible to obtain a total height of 16, so the answer is 1. 题意:给定n个数,问:任意i个数的和sum(i<=n)与 h的差最小为多少?(要求sum>=h) 题解:DFS
#include<iostream>
#include<algorithm>
#include<vector>
#define ll long long
using namespace std;
int a[];
int n,h,x=;
void dfs(int id,int sum)
{
if(id>=n)
{
if(sum>=h)
x=min(x,sum-h);
return;
}
else if(x==)
return;
else
{
dfs(id+,sum);
dfs(id+,sum+a[id]);
}
}
int main()
{
scanf("%d%d",&n,&h);
for(int i=;i<n;i++)
scanf("%d",&a[i]);
//sort(a,a+n);
dfs(,);
printf("%d\n",x);
return ;
}

Bookshelf 2 简单DFS的更多相关文章

  1. Red and Black(简单dfs)

    Red and Black Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

  2. POJ 1979 Red and Black (简单dfs)

    题目: 简单dfs,没什么好说的 代码: #include <iostream> using namespace std; typedef long long ll; #define IN ...

  3. POJ1573(Robot Motion)--简单模拟+简单dfs

    题目在这里 题意 : 问你按照图中所给的提示走,多少步能走出来??? 其实只要根据这个提示走下去就行了.模拟每一步就OK,因为下一步的操作和上一步一样,所以简单dfs.如果出现loop状态,只要记忆每 ...

  4. POJ1979 Red and Black (简单DFS)

    POJ1979 Description There is a rectangular room, covered with square tiles. Each tile is colored eit ...

  5. CF760 C. Pavel and barbecue 简单DFS

    LINK 题意:给出n个数,\(a_i\)代表下一步会移动到第\(a_i\)个位置,并继续进行操作,\(b_i\)1代表进行一次翻面操作,要求不管以哪个位置上开始,最后都能满足 1.到达过所有位置 2 ...

  6. uva 784 Maze Exploration(简单dfs)

    这道题看上去非常麻烦,什么迷宫啊.门之类的,事实上挺简单的,就是让把与 * 连通的都置为 # 包含 * , 直接dfs就能够了,只是我wa了好多次...最后居然是多读了一个换行.忘了加getchar( ...

  7. poj 2386:Lake Counting(简单DFS深搜)

    Lake Counting Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 18201   Accepted: 9192 De ...

  8. 题解报告:hdu 1312 Red and Black(简单dfs)

    Problem Description There is a rectangular room, covered with square tiles. Each tile is colored eit ...

  9. 【HDU-1045,Fire Net-纯暴力简单DFS】

    原题链接:点击!   大致题意:白块表示可以放置炮台的位置——每个炮台可以攻击到上下左右的直线上的炮台(也就是说在它的上下左右直线上不可以再放置炮台,避免引起互相攻击),黑块表示隔离墙的位置——不可放 ...

随机推荐

  1. 模拟服务容器Ioc

    服务容器是一个用于管理类依赖和执行依赖注入的强大工具. 一个类要被容器所能够提取,必须要先注册至这个容器.既然称这个容器叫做服务容器,那么我们需要某个服务,就得先注册.绑定这个服务到容器,那么提供服务 ...

  2. py related issues

    在python中安装包出现Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) pip inst ...

  3. 将图片转化为base64编码字符串

    pom依赖 <dependency> <groupId>org.ops4j.base</groupId> <artifactId>ops4j-base- ...

  4. java中如何修改事务的隔离级别

    事务的特性: 原子性(Atomicity)原子性是指事务是一个不可分割的工作单位,事务中的操作要么都发生,要么都不发生. (多条语句要么都成功,要么都失败.) 一致性(Consistency)事务前后 ...

  5. 107、Java中String类之判断开头或结尾

    01.代码如下: package TIANPAN; /** * 此处为文档注释 * * @author 田攀 微信382477247 */ public class TestDemo { public ...

  6. Laplacian Mesh Editing 拉普拉斯形变(待回学校更新)

    前言 因为实验需要用到拉普拉斯形变,但找了好久找到一个非常适合入门的资料.再此记录下我的学习过程,也算搬运翻译过来. Introduction / Basic Laplacian Mesh Repre ...

  7. Mini_Linux需要搭的环境

    1.bash:ifconfig:command not found sudo yum install -y net-tools 2.如果Linux系统是通过复制得到  需要更改hostname vi ...

  8. eclipse 编辑窗口不见了(打开左边的java、xml文件,中间不会显示代码)

    参考:https://blog.csdn.net/u012062810/article/details/46729779

  9. java并发初探CyclicBarrier

    java并发初探CyclicBarrier CyclicBarrier的作用 CyclicBarrier,"循环屏障"的作用就是一系列的线程等待直至达到屏障的"瓶颈点&q ...

  10. 如何在adapter 中调用activity的方法

    如何在adapter 中调用activity的方法 2015-08-07 17:06匿名 | 浏览 808 次  iWorkjavaAndroid public class HistoryData e ...