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 ...
随机推荐
- E20170911-hm
specification n. 规格; 说明书; 详述;
- Python 44 前端概述 、三剑客 、常用标签与分类
1.前端三剑客是哪三位?文件的后缀内容?在前端开发中的功能是什么? HTML: .htm .html 内容 CSS: .css 效果 JS: .js 行为 2.简述三剑客的主要 ...
- gitlab克隆报错:remote: HTTP Basic: Access denied;remote: You must use a personal access token with ‘api’ scope for Git over HTTP.
错误: remote: HTTP Basic: Access denied remote: You must use a personal access token with ‘api’ scope ...
- 五分钟学习React(六):元素(Element)和组件(Component)
俗话说"万丈高楼平地起",从这一期开始,我们将使用基于Webpack+Babel的React学习React框架中的一些基础概念.在学习React的过程中经常会把Element.Cl ...
- Ionic3 环境搭建以及基础配置实现(更新中)
GitHub:https://github.com/Teloi 环境配置输入以下命令安装 Ionic (如果刚才设置了淘宝镜像源,可以使用 cnpm 代替 npm):npm install -g io ...
- 【Linux】磁盘分区
我们在Linux操作过程中,可能会遇到磁盘分区的问题.这篇文章是对/dev/sdb 这块磁盘进行分区. linux分区不同于windows,linux下硬盘设备名为(IDE硬盘为hdx(x为从a—d) ...
- MatLab之HDL coder
1 Workflow The workflow for applying HDL code generation to the hardware design process requires the ...
- java web设置全局context参数
先在生成的web.xml文件中配置全局参数变量(Parameter:参数) <web-app> <context-param> 设置parameter(参数)的识别名字为adm ...
- Java---23种设计模式(九)------组合模式
一.什么是组合模式 组合模式(Composite Pattern),又叫部分整体模式,是用于把一组相似的对象当作一个单一的对象. 组合模式依据树形结构来组合对象,用来表示部分以及整体层次. 这种类型的 ...
- C#第十五节课
函数复习 using System;using System.Collections.Generic;using System.Linq;using System.Text;using System. ...