题意:给出n头牛的身高,以及一个书架的高度,问怎样选取牛,使得它们的高的和超过书架的高度最小。

将背包容量转化为所有牛的身高之和,就可以用01背包来做===

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define maxn 2000005
using namespace std;
int dp[maxn],h[];
int main()
{
int n,b,i,j,sum=,tmp=;
scanf("%d %d",&n,&b);
for(i=;i<=n;i++)
{
scanf("%d",&h[i]);
sum+=h[i];
}
for(i=;i<=n;i++)
{
for(j=sum;j>=h[i];j--)
{
dp[j]=max(dp[j],dp[j-h[i]]+h[i]);
{
if(dp[j]>=b&&j<tmp)
tmp=j;
}
}
}
printf("%d\n",tmp-b);
}

POJ 3628 Bookshelf 2【01背包】的更多相关文章

  1. POJ 3628 Bookshelf 2 0-1背包

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

  2. POJ 3628 Bookshelf 2 (01背包)

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

  3. POJ 3628 Bookshelf2(0-1背包)

    http://poj.org/problem?id=3628 题意:给出一个高度H和n个牛的高度,要求把牛堆叠起来达到H,求出该高度和H的最小差. 思路:首先我们计算出牛的总高度sum,sum-H就相 ...

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

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

  5. POJ 3211 Washing Clothes(01背包)

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

  6. poj 3628 Bookshelf 2

    http://poj.org/problem?id=3628 01背包 #include <cstdio> #include <iostream> #include <c ...

  7. POJ3628 Bookshelf 2(01背包+dfs)

    Bookshelf 2 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8745   Accepted: 3974 Descr ...

  8. [POJ 2184]--Cow Exhibition(0-1背包变形)

    题目链接:http://poj.org/problem?id=2184 Cow Exhibition Time Limit: 1000MS   Memory Limit: 65536K Total S ...

  9. POJ 3624 Charm Bracelet(01背包裸题)

    Charm Bracelet Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 38909   Accepted: 16862 ...

随机推荐

  1. HDU 2669 Romantic(扩展欧几里德, 数学题)

    题目 //第一眼看题目觉得好熟悉,但是还是没想起来//洪湖来写不出来去看了解题报告,发现是裸的 扩展欧几里得 - - /* //扩展欧几里得算法(求 ax+by=gcd )//返回d=gcd(a,b) ...

  2. POJ 3276

    Face The Right Way Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 2193   Accepted: 103 ...

  3. lightoj 1408 Batting Practice (概率问题,求期望,推公式)

    题意:一个人若连续进k1个球或连续不进k2个球,游戏结束,给出这个人不进球的概率p(注意:是不进球!!!),求到游戏结束时这个投球个数的期望. 不进球概率为p,进概率 q=1-p.设 f[i] 表示连 ...

  4. JDBC 程序的常见错误及调试方法

    详细介绍:http://dev.mysql.com/doc/refman/5.5/en/error-handling.html http://dev.mysql.com/doc/refman/5.5/ ...

  5. ubuntu系统使用minicom终端操作说明

    http://blog.chinaunix.net/uid-22030783-id-3350834.html 在linux下,使用minicom作为串口终端工具,默认的串口设备是/dev/ttyS0, ...

  6. iOS开发--应用程序上线

    iOS应用上线 http://www.jianshu.com/p/ffddc5e5f0b9 iOS真机测试 http://www.jianshu.com/p/986e02d38f1b iOS应用程序打 ...

  7. Java笔记——面向切面编程(AOP模式)

    原文:http://www.cnblogs.com/yanbincn/archive/2012/06/01/2530377.html Aspect Oriented Programming  面向切面 ...

  8. Android 广播(内部类)

    1.广播定义在一个单独的文件中 源码: public class MessageReceiver extends BroadcastReceiver{ @Override public void on ...

  9. 机器人学 —— 机器人视觉(Bundle Adjustment)

    今天完成了机器人视觉的所有课程以及作业,确实是受益匪浅啊! 最后一个话题是Bundle Adjustment. 机器人视觉学中,最顶尖的方法. 1.基于非线性优化的相机位姿估计 之前已经在拟合一篇中, ...

  10. MyBatis实现SaveOrUpdate

    这篇文章主要讲如何通过xml方式实现SaveOrUpdate,但是仍然建议在Service中实现. 例子 <insert id="saveOrUpdate" > < ...