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 动态规划法题解的更多相关文章

  1. DIjkstra(反向边) POJ 3268 Silver Cow Party || POJ 1511 Invitation Cards

    题目传送门 1 2 题意:有向图,所有点先走到x点,在从x点返回,问其中最大的某点最短路程 分析:对图正反都跑一次最短路,开两个数组记录x到其余点的距离,这样就能求出来的最短路以及回去的最短路. PO ...

  2. POJ 3268 Silver Cow Party (最短路径)

    POJ 3268 Silver Cow Party (最短路径) Description One cow from each of N farms (1 ≤ N ≤ 1000) convenientl ...

  3. POJ 1163 The Triangle DP题解

    寻找路径,动态规划法题解. 本题和Leetcode的triangle题目几乎相同一样的,本题要求的是找到最大路径和. 逆向思维.从底往上查找起就能够了. 由于从上往下能够扩展到非常多路径.而从下往上个 ...

  4. POJ 3268——Silver Cow Party——————【最短路、Dijkstra、反向建图】

    Silver Cow Party Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Su ...

  5. POJ 3268 Silver Cow Party 最短路—dijkstra算法的优化。

    POJ 3268 Silver Cow Party Description One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbe ...

  6. poj 2431 Expedition 贪心 优先队列 题解《挑战程序设计竞赛》

    地址 http://poj.org/problem?id=2431 题解 朴素想法就是dfs 经过该点的时候决定是否加油 中间加了一点剪枝 如果加油次数已经比已知最少的加油次数要大或者等于了 那么就剪 ...

  7. poj 1064 Cable master 二分 题解《挑战程序设计竞赛》

    地址 http://poj.org/problem?id=1064 题解 二分即可 其实 对于输入与精度计算不是很在行 老是被卡精度 后来学习了一个函数 floor 向负无穷取整 才能ac 代码如下 ...

  8. POJ 3268 Silver Cow Party(最短路&Dijkstra)题解

    题意:有n个地点,有m条路,问从所有点走到指定点x再走回去的最短路中的最长路径 思路:用Floyd超时的,这里用的Dijkstra. Dijkstra感觉和Prim和Kruskal的思路很像啊.我们把 ...

  9. POJ 3628 Bookshelf 2 题解

    本题解法非常多,由于给出的数据特殊性故此能够使用DFS和BFS,也能够使用01背包DP思想来解. 由于一般大家都使用DFS,这里使用非常少人使用的BFS.缺点是比DFS更加耗内存,只是长处是速度比DF ...

随机推荐

  1. [Web 前端] React-router4简约教程

    cp from : http://react-china.org/t/react-router4/15843/1 React-router和React-router-dom的选择 很多刚使用react ...

  2. [Android Pro] 组件化:企业级大型项目必经之路

    cp : https://www.csdn.net/article/2011-02-11/291667 摘要:超过一年以上.活跃开发的项目往往到后期陷入了一些共性的问题: 构建速度慢,往往生成一次最终 ...

  3. [转]memcached+magent实现memcached集群

    From : http://www.cnblogs.com/happyday56/p/3461113.html 首先说明下memcached存在如下问题 本身没有内置分布式功能,无法实现使用多台Mem ...

  4. [转]php curl 设置host curl_setopt CURLOPT_HTTPHEADER 指定host

    From : http://digdeeply.org/archives/10132139.html 我们在开发测试时,有时web服务器会绑定一个域名,但是因为dns是无法解析的,我们需要设置host ...

  5. uva 400 Unix ls 文件输出排版 排序题

    这题的需要注意的地方就是计算行数与列数,以及输出的控制. 题目要求每一列都要有能够容纳最长文件名的空间,两列之间要留两个空格,每一行不能超过60. 简单计算下即可. 输出时我用循环输出空格来解决对齐的 ...

  6. go语言之进阶篇json解析到结构体

    1.json解析到结构体 示例: package main import ( "encoding/json" "fmt" ) type IT struct { ...

  7. iOS开发-Reachability实时检测Wifi,2G/3G/4G/网络状态

    最近遇到一个功能就是根据用户当前的网络状,用户未联网需要提示一下,如果是Wifi可以推荐一些图片新闻,如果是3G模式设置为无图的模式,获取网络状态比较简单,毕竟中国现在的流量还是一个比较贵的状态,哪天 ...

  8. 理解SVG图片标签的viewport、viewBox、preserveAspectRatio缩放

    一.viewport 表示SVG可见区域的大小,或者可以想象成舞台大小,画布大小. <svg width="></svg> 上面的SVG代码定义了一个视区,宽500单 ...

  9. MongoDB 通过配置文件启动

    mongod启动命令需指定一些参数启动服务,我们通过配置文件的方式配置这些参数加载配置. ./bin/mongod --config ./conf/mongodb.conf(或./bin/mongod ...

  10. 【Java】Java-XML解析利器-SAX-高性能-易用

    Java-XML解析利器-SAX-高性能-易用 java xml 大_百度搜索 (3)java处理比较大的xml文件 - SegmentFault How to read UTF-8 XML file ...