POJ 3268 Bookshelf 2 动态规划法题解
Description
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.
Input
* Line 1: Two space-separated integers: N and B
* Lines 2..N+1: Line i+1 contains a single integer: Hi
Output
* 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.
Sample Input
5 16
3
1
3
5
6
Sample Output
1
Source
解题思路:
1 确定可能的最大高度sum,就是全部的cow加起来的高度
2 依据动态规划法。求解1到最大高度sum之间的可能解
3 找到比B(书架高度)的最低高度,可能和B一致。
#include <stdio.h>
#include <vector>
#include <limits.h>
#include <string.h>
#include <algorithm>
using namespace std; const int MAX_N = 21, MAX_H = 1000000;
int cow[MAX_N];
bool height[MAX_N*MAX_H]; int getMinHeight(int N, int B, int sum)//B < sum
{
fill(height, height+sum+1, false);
height[0] = true;
for (int i = 0; i < N; i++)
{
for (int j = sum; j >= cow[i]; j--)
{
if (height[j-cow[i]]) height[j] = true;
}
}
int ans = B;
for (; ans <= sum && !height[ans]; ans++) {} return ans;
} int main()
{
int N, B, sum;
while (~scanf("%d %d", &N, &B))
{
sum = 0;
for (int i = 0; i < N; i++)
{
scanf("%d", cow+i);
sum += cow[i];
}
printf("%d\n", getMinHeight(N, B, sum)-B);
}
return 0;
}
POJ 3268 Bookshelf 2 动态规划法题解的更多相关文章
- DIjkstra(反向边) POJ 3268 Silver Cow Party || POJ 1511 Invitation Cards
题目传送门 1 2 题意:有向图,所有点先走到x点,在从x点返回,问其中最大的某点最短路程 分析:对图正反都跑一次最短路,开两个数组记录x到其余点的距离,这样就能求出来的最短路以及回去的最短路. PO ...
- POJ 3268 Silver Cow Party (最短路径)
POJ 3268 Silver Cow Party (最短路径) Description One cow from each of N farms (1 ≤ N ≤ 1000) convenientl ...
- POJ 1163 The Triangle DP题解
寻找路径,动态规划法题解. 本题和Leetcode的triangle题目几乎相同一样的,本题要求的是找到最大路径和. 逆向思维.从底往上查找起就能够了. 由于从上往下能够扩展到非常多路径.而从下往上个 ...
- POJ 3268——Silver Cow Party——————【最短路、Dijkstra、反向建图】
Silver Cow Party Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Su ...
- POJ 3268 Silver Cow Party 最短路—dijkstra算法的优化。
POJ 3268 Silver Cow Party Description One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbe ...
- poj 2431 Expedition 贪心 优先队列 题解《挑战程序设计竞赛》
地址 http://poj.org/problem?id=2431 题解 朴素想法就是dfs 经过该点的时候决定是否加油 中间加了一点剪枝 如果加油次数已经比已知最少的加油次数要大或者等于了 那么就剪 ...
- poj 1064 Cable master 二分 题解《挑战程序设计竞赛》
地址 http://poj.org/problem?id=1064 题解 二分即可 其实 对于输入与精度计算不是很在行 老是被卡精度 后来学习了一个函数 floor 向负无穷取整 才能ac 代码如下 ...
- POJ 3268 Silver Cow Party(最短路&Dijkstra)题解
题意:有n个地点,有m条路,问从所有点走到指定点x再走回去的最短路中的最长路径 思路:用Floyd超时的,这里用的Dijkstra. Dijkstra感觉和Prim和Kruskal的思路很像啊.我们把 ...
- POJ 3628 Bookshelf 2 题解
本题解法非常多,由于给出的数据特殊性故此能够使用DFS和BFS,也能够使用01背包DP思想来解. 由于一般大家都使用DFS,这里使用非常少人使用的BFS.缺点是比DFS更加耗内存,只是长处是速度比DF ...
随机推荐
- 制作高仿QQ的聊天系统(上)—— 布局文件 & 减少过度绘制
由于没有自己的服务器,我就找了个能实现双方通信的SDK,这个SDK是友盟的用户反馈SDK.本系列的博文关注的不是网络通信,而是如何在网络通信机制已经做好的情况下,做出一个可用的聊天系统.其实,刚开始做 ...
- 低版本系统兼容的ActionBar(四)添加Tab+添加自定义的Tab视图+Fragment
在ActionBar中添加Tab是很有用的技巧.在support V7库的支持下,我们几乎可以用和之前一样的方式来添加Tab,对于Tab来说,我们可以和MenuItem一样,给他定义自己的视图.我这里 ...
- CATEGORICAL, ORDINAL AND INTERVAL VARIABLES
WHAT IS THE DIFFERENCE BETWEEN CATEGORICAL, ORDINAL AND INTERVAL VARIABLES? In talking about variabl ...
- [转]linux最新分区方案
FROM : http://www.cnblogs.com/chenlulouis/archive/2009/08/27/1554983.html 我的服务器是500G.最重要的是/var分区一定要大 ...
- SQL Server中取汉字拼音的函数
)) ) ) ) ) ), py end return @pinyin END GOSELECT dbo.fn_GetP ...
- 算法:快速排序实现 & 定制比较函数
1. 快速排序基本算法 #include<stdio.h> ; int quick_sort(int *a, int start, int end){ if (start >= en ...
- Go语言之进阶篇获取文件属性
1.获取文件属性 示例: get_file_attribute.go package main import ( "fmt" "os" ) func main( ...
- (转)Esfog_UnityShader教程_UnityShader语法实例浅析
距离上次首篇前言已经有一段时间了,一直比较忙,今天是周末不可以再拖了,经过我一段时间的考虑,我决定这一系列的教程会避免过于深入细节,一来可以避免一些同学被误导,二来会避免文章过于冗长难读, 三来可以让 ...
- python中read() readline()以及readlines()用法
[转自:http://www.ibm.com/developerworks/cn/linux/sdk/python/python-5/index.html#N1004E] 我们谈到“文本处理”时,我们 ...
- How to Redirect in ASPNET Web API
You could set the Location header: public HttpResponseMessage Get() { var response = Request.Creat ...