POJ 3628 Bookshelf 2 题解
本题解法非常多,由于给出的数据特殊性故此能够使用DFS和BFS,也能够使用01背包DP思想来解。
由于一般大家都使用DFS,这里使用非常少人使用的BFS。缺点是比DFS更加耗内存,只是长处是速度比DFS快。
当然也比DFS难写点:
int N, B;
int Height[21];
inline int mMin(int a, int b) { return a > b? b : a; }
inline int mMax(int a, int b) { return a < b? b : a; } int bfs()
{
vector<int> vStk;
vStk.push_back(0); int id = 0;
int mh = INT_MAX;
while (id < N)
{
for (int i = (int)vStk.size() - 1; i >= 0 ; i--)
{
int h = vStk[i] + Height[id];
if (h >= B) mh = mMin(mh, h-B);
vStk.push_back(h);
}//缺点。会消耗大量的内存。最后vector会很大,长处,速度比dfs快
id++;
}
return mh;
} int main()
{
while (scanf("%d %d", &N, &B) != EOF)
{
for (int i = 0; i < N; i++)
{
scanf("%d", &Height[i]);
}
printf("%d\n", bfs());
}
return 0;
}
POJ 3628 Bookshelf 2 题解的更多相关文章
- poj 3628 Bookshelf 2
http://poj.org/problem?id=3628 01背包 #include <cstdio> #include <iostream> #include <c ...
- POJ 3628 Bookshelf 2 0-1背包
传送门:http://poj.org/problem?id=3628 题目看了老半天,牛来叠罗汉- -|||和书架什么关系啊.. 大意是:一群牛来叠罗汉,求超过书架的最小高度. 0-1背包的问题,对于 ...
- POJ 3628 Bookshelf 2(01背包)
Bookshelf 2 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9488 Accepted: 4311 Descr ...
- POJ 3628 Bookshelf 2 (01背包)
Bookshelf 2 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7496 Accepted: 3451 Descr ...
- POJ 3628 Bookshelf 2【背包型DFS/选or不选】
Bookshelf 2 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11105 Accepted: 4928 Desc ...
- POJ 3628 Bookshelf 2【01背包】
题意:给出n头牛的身高,以及一个书架的高度,问怎样选取牛,使得它们的高的和超过书架的高度最小. 将背包容量转化为所有牛的身高之和,就可以用01背包来做=== #include<iostream& ...
- poj 3628 Bookshelf 2 基本01背包
题目大意:FJ有n头奶牛,和一个高为h的架子,给出每头奶牛高度,求使奶牛叠加起来超过架子的最低高度是多少. 题目思路:求出奶牛叠加能达到的所有高度,并用dp[]保存,最后进行遍历,找出与h差最小的dp ...
- POJ 2823 Sliding Window 题解
POJ 2823 Sliding Window 题解 Description An array of size n ≤ 106 is given to you. There is a sliding ...
- POJ 3268 Bookshelf 2 动态规划法题解
Description Farmer John recently bought another bookshelf for the cow library, but the shelf is gett ...
随机推荐
- centos安装lamp步骤还可以
1. 用yum安装Apache,Mysql,PHP. 1.1安装Apache yum install httpd httpd-devel 安装完成后,用/etc/init.d/httpd start ...
- nodejs express开发
用NodeJS+Express开发WEB应用---第一篇 大漠穷秋2014-03-28 预热 为了对后面的内容理解更加透彻,推荐首先阅读下面这篇很好的文章: http://www.nodebeginn ...
- 练习2 及pl/sql
Rownum 如果不是对主键排序是不会变得 -查询没有学分的学生信息 --SELECT * FROM z_student zs WHERE zs.code NOT IN (SELECT DISTINC ...
- BZOJ 4808 二分图最大独立集
思路: 棋盘是个二分图 那就把一个可以走的白点 向所有可以走的黑点连边 跑一个最大匹配 (匹配上了就代表这两个点不能共存) 最大独立集=sum-最大匹配 //By SiriusRen #incl ...
- spring注解、aop(二)
使用注解配置spring 1.导入 spring-aop-5.0.6.RELEASE.jar包 2.为主配置文件引入新的命名空间 xmlns:context="http://www.spri ...
- .net core2.0 读取appsettings.json
一.在start.up中添加注入 二.使用
- SQLServer2008 关于数值字段列的累计
create table #temp20110610( id int identity(1,1), date varchar(8), qty float) insert int ...
- 使用cnblogs发布第一篇文章,HelloWorld
HelloWorld! 瞅瞅源码的样式,嗯,语法高亮还是可以的,辨识度还是挺高的. <!DOCTYPE html> <html> <head> <meta c ...
- python--6、logging模块
logging 可用的日志级别: debug 10 info 20 warning 30 error 40 critical 50 logging默认参数: 默认日志级别是warning. 默认情况日 ...
- ajax-工作原理,包含技术,缺陷
1:原理 2:包含技术 3:缺陷