链接: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. android.view.WindowManager$BadTokenException 崩掉

    问题: 以前的项目,今天打开运行,Activity刚打开的时候,点开一个弹窗是好的,但是再点到另一个界面的时候,返回,再点弹窗就崩了. 解决: 网上查了一下,发现出现这个问题的还特别多,大体如下: 1 ...

  2. Mongodb - 解决 ( aggregate聚合管道 ) $match 根据 id 匹配 返回 [ ] 的问题

    需要对 id 进行转换 const mongoose = require('mongoose') var ObjectId = mongoose.Types.ObjectId;   await Use ...

  3. Day11 - N - Game HDU - 3389

    题目链接 题意是说有1到n个标号的盒子,选择一个非空的盒子A,B是否空无所谓,满足(A+B)%2=1,(A+B)%3=0,A>B 解上面的同余方程组,最小解为3,循环为2*3=6,那我们可以把前 ...

  4. JS回弹原理-高级

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  5. python中sorted方法和列表的sort方法使用

    一.基本形式 列表有自己的sort方法,器对列表进行原值排序,既然是原址排序,那显然元组不可能拥有这个方法,因为元组是不可修改的. 排序,数字.字符串按照ASCII,中文按照unicode从小到大排序 ...

  6. 吴裕雄 Bootstrap 前端框架开发——Bootstrap 按钮:块级按钮(拉伸至父元素100%的宽度)

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  7. request-html

    目录 基本使用 获取链接( links 与 absolute_links ) CSS 选择器与 XPATH 支持 JavaScript 自定义 User-Agent 模拟表单提交(POST) asyn ...

  8. ADV-292 计算行列式 java

    问题描述 //据说很多人的题目会有一大堆废话,本傻×就不在这里废话了. 给定一个N×N的矩阵A,求|A|. 输入格式 第一行一个正整数N. 接下来N行,每行N个整数,第i行第j个数字表示A[i][j] ...

  9. java学习-初级入门-面向对象⑤-类与对象-类与对象的定义和使用3

    这次我们要做一个日期类Date类 主要目的是    1.  熟悉-->构造不同参数的函数  2.善于利用已有的函数!! 题目要求: Date类要求 可设定年月日 可转换为字符串,并可指定分隔符, ...

  10. hadoop的扩容

    一.横向扩容(参见:https://www.cnblogs.com/yangy1/p/12362565.html) 现在在此基础上再添加一个节点 1.克隆一台主机hdp03(克隆hdp02) 修改ip ...