dp或dfs(01背包问题)
链接:https://ac.nowcoder.com/acm/contest/993/C
来源:牛客网
题意:n头牛,给出它们的H高度,问这些牛的高度叠加起来大于等于书架高度,问叠加后的高度与书架的差值最小为多少?
1解法:01背包容量枚举从书架高度到所有牛高度总和的高度,遍历从书架高度容量开始,取可满足容量的最小值。
dp【j】表示背包容量为j时,所能装下的最大物品重量。
//#include <bits/stdc++.h>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <algorithm>
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <stdio.h>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <string.h>
#include <vector>
#define ME(x , y) memset(x , y , sizeof(x))
#define SF(n) scanf("%d" , &n)
#define rep(i , n) for(int i = 0 ; i < n ; i ++)
#define INF 0x3f3f3f3f
#define mod 1000000007
#define PI acos(-1)
using namespace std;
typedef long long ll ;
int w[20] , dp[1000009]; int main()
{
int n , h ;
scanf("%d%d" , &n , &h);
int sum = 0 ;
for(int i = 1 ; i <= n ; i++)
{
scanf("%d" , &w[i]);
sum += w[i];
}
memset(dp, 0 , sizeof(dp));
for(int i = 1 ; i <= n ; i++)
{
for(int j = sum ; j >= w[i] ; j--)
{
dp[j] = max(dp[j] , dp[j-w[i]]+w[i]);
}
}
int ans = INF ;
for(int i = h ; i <= sum ; i++)
{
if(dp[i] >= h)
ans = min(ans , abs(dp[i] - h));
}
cout << ans << endl ; return 0;
}
2解法:因为牛数量较少,可以dfs搜索所有可能的牛叠加的高度情况

//#include <bits/stdc++.h>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <algorithm>
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <stdio.h>
#include <queue>
#include <stack>;
#include <map>
#include <set>
#include <ctype.h>
#include <string.h>
#include <vector>
#define ME(x , y) memset(x , y , sizeof(x))
#define SF(n) scanf("%d" , &n)
#define rep(i , n) for(int i = 0 ; i < n ; i ++)
#define INF 0x3f3f3f3f
#define mod 10
#define PI acos(-1)
using namespace std;
typedef long long ll ;
int n , m , ans ;
int a[];
void dfs(int s , int l)
{
if(s >= m)
{
ans = min(ans , s - m);
return ;
}
for(int i = l ; i < n ; i++)
{
dfs(s+a[i] , i+);
}
} int main()
{ scanf("%d%d" , &n , &m);
for(int i = ; i < n ; i++)
{
scanf("%d" , &a[i]);
}
ans = INF ;
dfs( , );
cout << ans << endl ; return ;
}
dp或dfs(01背包问题)的更多相关文章
- DP动态规划之01背包问题
目录 问题描述 问题分析 问题求解 Java代码实现 优化方向一:时间方面:因为是j是整数是跳跃式的,可以选择性的填表. 思考二:处理j(背包容量),w(重量)不为整数的时候,因为j不为整数了,它就没 ...
- dp入门之01背包问题
...通过暴力手推得到的一点点感觉 动态规划是相对于贪心算法的一种取得最优解的算法,通过对每一步的取舍判断从 0 推到所拥有的第 n 件物品,每次判断可以列写出状态转移方程,通过记忆化相对暴力地取得最 ...
- 经典DP动规 0-1背包问题 二维与一维
先上代码 b站讲解视频 灯神讲背包 #include <iostream> #include <cstring> #include <algorithm> usin ...
- 01背包问题之2(dp)
01背包问题之2 有n个物品,重量和价值分别为wi和vi,从这些物品中挑选出重量不超过W的物品,求所有挑选方案中物品价值总和的最大值 限制条件: 1 <= n <= 100; 1 < ...
- 普通01背包问题(dp)
有n个物品,重量和价值分别为wi和vi,从这些物品中挑选出重量不超过W的物品,求所有挑选方案中物品价值总和的最大值 限制条件: 1 <= n <= 100; 1 <= wi,vi & ...
- 01背包问题的延伸即变形 (dp)
对于普通的01背包问题,如果修改限制条件的大小,让数据范围比较大的话,比如相比较重量而言,价值的范围比较小,我们可以试着修改dp的对象,之前的dp针对不同的重量限制计算最大的价值.这次用dp针对不同的 ...
- 动态规划(DP),0-1背包问题
题目链接:http://poj.org/problem?id=3624 1.p[i][j]表示,背包容量为j,从i,i+1,i+2,...,n的最优解. 2.递推公式 p[i][j]=max(p[i+ ...
- 九度OJ 1123:采药 (01背包、DP、DFS)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:2705 解决:1311 题目描述: 辰辰是个很有潜能.天资聪颖的孩子,他的梦想是称为世界上最伟大的医师. 为此,他想拜附近最有威望的医师为师 ...
- 01背包问题(dfs+剪枝)
01背包问题 dfs解法 #include <iostream> #include <cstring> #include <algorithm> #include ...
随机推荐
- hash和history
location.hash="aaa" history.pushState({},'', "home") history.replaceState() hist ...
- python基础--文件的操作
#r w a 文件读取操作 默认打开为读操作 #f=open('coldplay.txt','r',encoding="utf-8")#open函数默认已系统编码方式打开windo ...
- hInstWtsapi32 = LoadLibrary("Wtsapi32.dll");
https://www.cnblogs.com/beawesome/p/6473668.html 进程枚举 之类
- django之csrf_exempt解决跨域请求的问题
一: from django.views.decorators.csrf import csrf_exempt # 获取微信返回的code信息 @csrf_exempt def wechat_auth ...
- js undefined三目运算
js ajax传值中 "id":$('#id').val(), 如果#id不存在,使用$('#id').val()||‘’,可避免向后台传入undefined
- OC—类的设计和NSString
经过前一段时间C语言 的学习,从这周开始正式步入OC的学习 OC中类的定义:同一类事物的抽象,对象则与之相反,是抽象的类的具体化. OC中定义属性字段时通常在元素前面加上_如 NSString * _ ...
- ht-4 hashmap特性
一.hashmap底层原理: hashmap调用默认构造方法会产生一个默认底层是长度为16的Entry数组,首先调用key的hasCode()方法来得到一个整数, int hash = hash(ke ...
- The Factor
The Factor Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total ...
- 使用MAC OS X进行PHP开发的一些建议和技巧
原创作品,允许转载,转载时请务必以超链接形式标明转载自:线筝 本文链接地址: 使用Mac OS X进行PHP开发的一些建议和技巧 用Mac OS X作为开发机已经有一年多的时间了,在这里写下自己的一些 ...
- Js获取屏幕宽度、高度
document.body.clientWidth ==> BODY对象宽度 document.body.clientHeight ==> BODY对象高度 document.docume ...