链接: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. 记一次安装体验:pwn工具

    几天前删了JDK,结果和VM在一个目录中,结果VM全没了,重安走起 记载一下安装虚拟机出现的问题,官网一个字....慢,于是找了百度网盘,据说win10版本太低没法用,我就下了vw15.5.0(建立在 ...

  2. ProgressBarForm 进度条

    ProgressBarForm public partial class ProgressBarForm : Form { private Panel panel1 = new System.Wind ...

  3. Android studio For Mac 安装

    简介: Google在2013的I/O开发者大会上正式对外宣布Android Studio将作为Android开发的主要IDE,它是基于IntelliJ IDEA打造的一款专门开发Android的神器 ...

  4. SRSniffer抓包工具的使用

    1.打开SRSniffer软件 2.按照1-->2-->3依次点击 3.点击左侧的启动监听按钮 4.打开要记录api的软件,查看效果

  5. MySQL 之与Python相关

    一.python 操作mysql import pymysql ''' # 1.基本语法 # (1) 连接数据库 # conn = pymysql.connect(host = "ip地址& ...

  6. CSS - icon图标(icon font)

    1. 概念 这个小红点是图标,图标在CSS中实际上是字体. 2. 为什么出现本质是字体的图标? 2.1 图片增加了总文件的大小. 2.2 图片增加了额外的http请求,大大降低网页的性能. 2.3 图 ...

  7. classmethode,staticmethode

    目录 classmethod: staticmethod: 一般来说,要使用某个类的方法,需要先实例化一个对象再调用方法 而使用@staticmethod或@classmethod,就可以不需要实例化 ...

  8. 视频游戏的连击 [USACO12JAN](AC自动机+动态规划)

    传送门 默认大家都学过trie与AC自动机. 先求出fail,对于每个节点维护一个sum,sum[u]待表从根到u所形成的字符串能拿到几分.显然sum[u]=sum[fail] + (u是几个字符串的 ...

  9. Linux命令:history命令历史的管理及用法

    bash可以保存的过去曾经执行过的命令.当某个用户登录到shell中,会读取该用户家目录中的~/.bash_history文件,并将历史命令列表保存到内存中.当用户退出当前shell时,会将内存中的历 ...

  10. pytorch 调整tensor的维度位置

    target.permute([0, 3, 1, 2]) 一定要使用permute以及中括号 一些在我这里没起到作用的网上的例子: 1. https://blog.csdn.net/zouxiaolv ...