链接: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. CSP-J2019 加工零件

    Background: 之前 $noip $死了,泥萌都说 \(noip SPFA\) 了,现在 \(noip\) 复活了,所以 \(SPFA\) 也复活了. (注:这里的 \(noip\) 跟 \( ...

  2. 1 (msql实战) 基础架构

    mysql> select * from T where ID=10: 我们看到的只是输入一条语句,返回一个结果,却不知道这条语句在 MySQL 内部的执行过程. 所以今天我想和你一起把 MyS ...

  3. [RoarCTF2019]forensic

    拿到raw文件拖到kali里,首先看镜像信息. volatility -f /root/mem.raw imageinfo 用建议的profile,Win7SP1x86.先查看下内存中的进程 vola ...

  4. luogu P2754 [CTSC1999]家园

    本题是分层图最大流问题,相当于按时间拆点,每个当前点向下一点的下一时间层连点,每一层有n+1个点 #include<bits/stdc++.h> using namespace std; ...

  5. 手机号----IP api

    /* *手机号码API */ $fPArr = iconv("gbk","utf-8",file_get_contents($fphone)); echo $f ...

  6. 眼镜蛇扫描器2.3【旁注,目录扫描,md5解密,后台破解,0day】

    功能: 1):旁注查询 引用站长工具 2):目录扫描  支持ASP PHP  JSP  ASPX  以及普遍存在漏洞的目录 速度还好.几乎和御剑差不多 3):0day扫描, 仿的熊眼  0day是 C ...

  7. 观察者设计模式(C#委托和事件的使用)

    观察者设计模式定义了对象间的一种一对多的依赖关系,以便一个对象的状态发生变化时,所有依赖于它的对象都得到通知并自动刷新.在现实生活中的可见观察者模式,例如,微信中的订阅号,订阅博客和QQ微博中关注好友 ...

  8. SqlParameter 类

    SqlParameter 类 表示 SqlCommand 的参数,也可以是它到 DataSet 列的映射.无法继承此类. 命名空间: System.Data.SqlClient 程序集: System ...

  9. Angular4之时间管道

    {{时间戳 |date:“yyyy/MM/dd HH:mm:ss”}} “YYYY/MM/DD”不可

  10. Flutter 使用json_model解析json生成dart文件

    一.json_serializable使用步骤 1.集成json_serializable pubspec.yaml 添加以下依赖 dependencies: json_annotation: ^2. ...