http://poj.org/problem?id=3628

题意:给出一个高度H和n个牛的高度,要求把牛堆叠起来达到H,求出该高度和H的最小差。

思路:首先我们计算出牛的总高度sum,sum-H就相当于一个背包容量,如果我们往里装高度正好等于了sum-H,也就是说明我们堆叠的牛的高度正好等于了H。

这样一来很好理解,就是计算在一个背包容量为sum-H的背包中最多能装多少。题目本身还是不难的,直接套用模板就行了。

 #include<iostream>
#include<algorithm>
#include<cstring>
using namespace std; const int maxn = +; int n, h;
int dp[maxn];
int sum; int w[maxn]; int main()
{
//freopen("D:\\txt.txt", "r", stdin);
while (cin >> n >> h && n && h)
{
//memset(dp, 0, sizeof(dp));
sum = ;
for (int i = ; i <= n; i++)
{
cin >> w[i];
sum += w[i];
}
int x = sum - h;
for (int i = ; i <= n; i++)
{
for (int j = x; j >= w[i]; j--)
dp[j] = max(dp[j], dp[j - w[i]] + w[i]);
}
cout << x-dp[x] << endl;
}
return ;
}

POJ 3628 Bookshelf2(0-1背包)的更多相关文章

  1. POJ 1745 【0/1 背包】

    题目链接:http://poj.org/problem?id=1745 Divisibility Time Limit: 1000MS   Memory Limit: 10000K Total Sub ...

  2. poj 3628 (搜索or背包)

    好久没看背包题目了!!!生疏了!!!! 这题是背包题!!!不过对于这题,解决方法还是搜索省时!!! 题意:第一行给你一个N和VV,接下来N行,每行一个数,求得是任选N个数组合求和,求组合的和大于VV而 ...

  3. POJ 3628 Bookshelf 2 0-1背包

    传送门:http://poj.org/problem?id=3628 题目看了老半天,牛来叠罗汉- -|||和书架什么关系啊.. 大意是:一群牛来叠罗汉,求超过书架的最小高度. 0-1背包的问题,对于 ...

  4. POJ 3628 Bookshelf 2 (01背包)

    Bookshelf 2 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7496   Accepted: 3451 Descr ...

  5. POJ 3628 Bookshelf 2【背包型DFS/选or不选】

    Bookshelf 2 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11105   Accepted: 4928 Desc ...

  6. POJ 1636 Prison rearrangement DFS+0/1背包

    题目链接: id=1636">POJ 1636 Prison rearrangement Prison rearrangement Time Limit: 3000MS   Memor ...

  7. POJ 3181 Dollar Dayz(全然背包+简单高精度加法)

    POJ 3181 Dollar Dayz(全然背包+简单高精度加法) id=3181">http://poj.org/problem?id=3181 题意: 给你K种硬币,每种硬币各自 ...

  8. POJ 3211 Washing Clothes(01背包)

    POJ 3211 Washing Clothes(01背包) http://poj.org/problem?id=3211 题意: 有m (1~10)种不同颜色的衣服总共n (1~100)件.Dear ...

  9. poj1417 带权并查集+0/1背包

    题意:有一个岛上住着一些神和魔,并且已知神和魔的数量,现在已知神总是说真话,魔总是说假话,有 n 个询问,问某个神或魔(身份未知),问题是问某个是神还是魔,根据他们的回答,问是否能够确定哪些是神哪些是 ...

随机推荐

  1. 008-spring cloud gateway-路由谓词RoutePredicate、RoutePredicateFactory

    一.概述 Spring Cloud Gateway将路由作为Spring WebFlux HandlerMapping基础结构的一部分进行匹配. Spring Cloud Gateway包含许多内置的 ...

  2. RN例子,发送http请求,日期选择

    发送http请求 let map = { method: 'post', headers: { token: '', 'Content-Type': 'application/json' }, bod ...

  3. 虚拟机VMware的网络设置出了问题会导致很多莫名的错误

    邪门地CentOS内软件安装失败问题:Xshell与虚拟机的各种连接失败:CentOS下eth0没显示ip地址. 这些原因竟然是一个,虚拟机VMware的网络设置出了问题.     恢复初始设置即可.

  4. ssm后台开发及发布

    本文详细讲解一下后台的创建及发布过程,包括踩过的坑 1:首先创建war包形式的maven工程 File>new>Maven project>Create a simple proje ...

  5. PHP Warning: preg_match(): JIT compilation failed: no more memory in

    PHP7.3出现如下错误:PHP Warning: preg_match(): JIT compilation failed: no more memory in ... 解决方案: 修改 /usr/ ...

  6. [LeetCode] 176. Second Highest Salary_Easy tag: SQL

    Write a SQL query to get the second highest salary from the Employee table. +----+--------+ | Id | S ...

  7. Selenium - Xpath 使用方法

    由于最新版火狐不在支持FireBug等开发工具,可以通过https://ftp.mozilla.org/pub/firefox/releases/下载49版本以下的火狐就可以增加Firebug等扩展了 ...

  8. qt用mingw编译时报错 multiple definition of

    网上相关回答不少,但过于简单,这里做一下记录. qt用mingw编译程序时报“multiple definition of …”这个错误,错误信息大概是如下图所示: 1 2 3 首先,检查自己的程序是 ...

  9. NC_Verilog中的工具ICC

    Cadence中的Incisive Comprehensive Coverage(ICC) solusion提供在仿真中的覆盖率分析. ICC中的覆盖率类型有两大类: 1)Code Coverage: ...

  10. jq table页二级联动

    <div class="layerRtb layerRtb-threecolumn"> <div class="clearfix layerRtb-he ...