Bookshelf 2 简单DFS
链接:https://ac.nowcoder.com/acm/contest/993/C
来源:牛客网
题目描述
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.
说明
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的更多相关文章
- Red and Black(简单dfs)
Red and Black Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
- POJ 1979 Red and Black (简单dfs)
题目: 简单dfs,没什么好说的 代码: #include <iostream> using namespace std; typedef long long ll; #define IN ...
- POJ1573(Robot Motion)--简单模拟+简单dfs
题目在这里 题意 : 问你按照图中所给的提示走,多少步能走出来??? 其实只要根据这个提示走下去就行了.模拟每一步就OK,因为下一步的操作和上一步一样,所以简单dfs.如果出现loop状态,只要记忆每 ...
- POJ1979 Red and Black (简单DFS)
POJ1979 Description There is a rectangular room, covered with square tiles. Each tile is colored eit ...
- CF760 C. Pavel and barbecue 简单DFS
LINK 题意:给出n个数,\(a_i\)代表下一步会移动到第\(a_i\)个位置,并继续进行操作,\(b_i\)1代表进行一次翻面操作,要求不管以哪个位置上开始,最后都能满足 1.到达过所有位置 2 ...
- uva 784 Maze Exploration(简单dfs)
这道题看上去非常麻烦,什么迷宫啊.门之类的,事实上挺简单的,就是让把与 * 连通的都置为 # 包含 * , 直接dfs就能够了,只是我wa了好多次...最后居然是多读了一个换行.忘了加getchar( ...
- poj 2386:Lake Counting(简单DFS深搜)
Lake Counting Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 18201 Accepted: 9192 De ...
- 题解报告:hdu 1312 Red and Black(简单dfs)
Problem Description There is a rectangular room, covered with square tiles. Each tile is colored eit ...
- 【HDU-1045,Fire Net-纯暴力简单DFS】
原题链接:点击! 大致题意:白块表示可以放置炮台的位置——每个炮台可以攻击到上下左右的直线上的炮台(也就是说在它的上下左右直线上不可以再放置炮台,避免引起互相攻击),黑块表示隔离墙的位置——不可放 ...
随机推荐
- 3_04_MSSQL课程_Ado.Net_.ExcuteReader()(SQLDataReader)
ExcuteNonQuery(); 返回影响的行数 ExcuteSacalar();返回第一行第一列 ExcuteReader(): Reader,指针,指向表的表头.只是指向,数据仍在数据库中. S ...
- scala命令行界面:help
:help 查看所有可用命令 scala> :helpAll commands can be abbreviated, e.g., :he instead of :help.:edit < ...
- Codeforces Round #586 (Div. 1 + Div. 2)D(二分图构建,思维,结论需数论证明)
#include<bits/stdc++.h>using namespace std;long long a[200007];vector<int>v[77];int main ...
- 阿里云服务器Ubantu16.04升级python3.6
从ppa上下载下载python3.6 sudo apt-get install software-properties-common#使用 “add-apt-repository” 脚本添加 ppa ...
- Python 基础之模块之math random time
一:math 数学模块import math#(1)ceil() 向上取整操作 (对比内置round)res = math.ceil(6.001) #注意精度损耗print(res)#(2)floo ...
- linux 从一台服务器向另台服务器复制文件
使用scp命令: sudo scp -P 2222 username@192.168.0.200:/home/db/db_data.sql.gz /home/db/db_data.sql.gz scp ...
- Victor and String[Bestcoder #52 1004](回文树)
题目描述 Victor喜欢玩字符串.他认为一个字符串是迷人的,当且仅当字符串是回文的.Victor想玩n次.每次他都会做以下四种操作中的一种.操作1:在字符串的开头添加一个字符 c.操作2:在字符串的 ...
- Linux-使用之vim出现的问题
参考来源: https://stackoverflow.com/questions/47667119/ycm-error-the-ycmd-server-shut-down-restart-wit-t ...
- LinkedList学习:API调用、栈、队列实现
参考的博客 Java 集合系列05之 LinkedList详细介绍(源码解析)和使用示例 如果你想详细的区了解容器知识以及本文讲的LinkedList,我推荐你去看这篇博客和这个做个的容器系列 Lin ...
- 图的Prim,Kruskal,Dijkstra,Floyd算法
代码部分有点问题,具体算法没问题, 最近期末考,要过段时间才会修改 //邻接矩阵,具体情况看上一篇的图的实现template<class T>class MGraph {public: ...