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

USACO 2007 December Bronze

解题思路:

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. D - Silver Cow Party POJ - 3268

    D - Silver Cow Party POJ - 3268 dijkstra 是 O(n2),堆优化一下, O(nlogn) 对每个点跑一次 dj, 取 max(dis(x->i)+dis( ...

  2. POJ 3628 Bookshelf 2

    POJ 3628 Bookshelf 2:http://poj.org/problem?id=3628 题意:有个书架,高度为B,现在FJ有N个奶牛,每个奶牛有个高度hi,现在将奶牛堆起来,使得堆起来 ...

  3. POJ 3735 Training little cats​ 题解 《挑战程序设计竞赛》

    为什么80%的码农都做不了架构师?>>>    POJ 3735 Training little cats调教猫咪:有n只饥渴的猫咪,现有一组羞耻Play,由k个操作组成,全部选自: ...

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

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

  5. POJ 魔兽世界之一:备战 题解

    POJ 魔兽世界之一:备战 题解 来源: POJ 注意: 总时间限制: 1000ms 内存限制: 65536kB 描述 魔兽世界的西面是红魔军的司令部,东面是蓝魔军的司令部.两个司令部之间是依次排列的 ...

  6. POJ 1061青蛙的约会题解

    POJ 1061青蛙的约会题解 网上似乎有不少此题的解法.我这个post和其他人的相比主要时想说下面几点. 1. 给出一个试图不死记硬背公式的思路; 2. 谈谈暴力解为什么看起来这么诱人,却无法通过; ...

  7. Silver Cow Party (POJ - 3268 )

    Silver Cow Party (POJ - 3268 ) 这道题是我做的最短路专题里的一道题,但我还没做这个,结果比赛就出了,真是.......... 题目: One cow from each ...

  8. POJ 3268 D-Silver Cow Party

    http://poj.org/problem?id=3268 Description One cow from each of N farms (1 ≤ N ≤ 1000) conveniently ...

  9. poj 3682 Bookshelf 2——背包问题(dfs)

    Bookshelf 2 Description Farmer John recently bought another bookshelf for the cow library, but the s ...

最新文章

  1. 2019春第二次课程设计实验报告
  2. 分类模型的性能评估——以SAS Logistic回归为例(1): 混淆矩阵
  3. 3-uboot-spl代码流程
  4. bom event周期_前端知识点总结——BOM
  5. 你面对以希望为名的绝望微笑
  6. 计算机二级宏相关例题,计算机等级考试二级Access练习题
  7. HTML使川锚标签,第1章HTML的基本标签祥解.ppt
  8. Create a Search Scope for a Sharepoint 2010 List or Library
  9. Atitit webclient httpclient技术总结 RestTemplate Atitit CateIT重要技术httpclient iduah2 impt 体系树路径:CS
  10. 我的联想拯救者y7000p怎么安装双系统Linux ubuntu 18.04呢?
  11. EHS法律法规的收集渠道
  12. 在c语言中 根据数据的组织形式,文件工作组织形式 文件的基本组织方式有哪几种?...
  13. 红米4A Android 版本,#MIUI#关于红米手机4高配版 Android版本适配的说明【miui9吧】_百度贴吧...
  14. 我的世界java百度什么电脑玩好_【我的世界】为了在龙芯电脑上玩Minecraft(我的世界)我做了什么_玩得好游戏攻略...
  15. 淘宝分布式数据库是如何实现高可用的
  16. MySQL 判断是否周末
  17. kaliddos教学
  18. Arduino温度传感器全系列使用详解
  19. 有数Bizhelp帮助文档在线制作软件官方
  20. EularProject 101:Optimum polynomial

热门文章

  1. shell建设告警系统
  2. kali 安装grub theme
  3. 多线程面试体系列(13):多线程同步内功心法——PV操作下
  4. Check Point在Google Play上发现大批感染Judy恶意软件的应用
  5. 数据结构基础知识(2)
  6. ajax教程 异步刷新验证,Ajax实现异步刷新验证用户名是否已存在的具体方法
  7. JQuery Mobile中特有事件和方法
  8. 《『若水新闻』客户端开发教程》——15代码编写(7)
  9. upc组队赛5 Election of Evil【搜索】
  10. 如何理解lower_bound/upper_bound