Bookshelf 2
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 9488   Accepted: 4311

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

一种做法是用dp[i]代表不超过i的可堆到最大程度,然后从m开始寻找第一个大于等于m的dp[i]就是答案,我用的是恰好装满的初始化条件,若可以出现一个恰好装满的解就输出……题目的S小于2000W其实比较大,1000W就差不多了

代码:

#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<sstream>
#include<cstring>
#include<bitset>
#include<cstdio>
#include<string>
#include<deque>
#include<stack>
#include<cmath>
#include<queue>
#include<set>
#include<map>
using namespace std;
#define INF 0x3f3f3f3f
#define CLR(x,y) memset(x,y,sizeof(x))
#define LC(x) (x<<1)
#define RC(x) ((x<<1)+1)
#define MID(x,y) ((x+y)>>1)
typedef pair<int,int> pii;
typedef long long LL;
const double PI=acos(-1.0);
const int N=10000010;
int dp[N];
int cow[30];
int n,m;
void zero_one_pack(int c,int w,int V)
{
for (int i=V; i>=c; --i)
if(dp[i-c]+w>dp[i])
dp[i]=dp[i-c]+w;
}
int main(void)
{
int i,ans;
while (~scanf("%d%d",&n,&m))
{
CLR(dp,-INF);
dp[0]=0;
int sum=0;
for (i=0; i<n; ++i)
{
scanf("%d",&cow[i]);
sum+=cow[i];
}
for (i=0; i<n; ++i)
zero_one_pack(cow[i],cow[i],sum);
for (i=m; i<=sum; ++i)
{
if(dp[i]>=0)
{
printf("%d\n",i-m);
break;
}
}
}
return 0;
}

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【01背包】

    题意:给出n头牛的身高,以及一个书架的高度,问怎样选取牛,使得它们的高的和超过书架的高度最小. 将背包容量转化为所有牛的身高之和,就可以用01背包来做=== #include<iostream& ...

  7. poj 3628 Bookshelf 2

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

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

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

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

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

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

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

随机推荐

  1. GLSL

    变量修饰符 修饰符给出了变量的特殊含义,GLSL中有如下修饰符: ·const – 声明一个编译期常量. ·attribute– 随不同顶点变化的全局变量,由OpenGL应用程序传给顶点shader. ...

  2. python为什么有私有方法和变量

    1. 访问安全,其实也没有决定的安全 >>> class humer(object): ... def __init__(self, name): ... self.name = n ...

  3. C++复制构造函数和赋值符的区别

    From  http://blog.csdn.net/randyjiawenjie/article/details/6666937 非常感谢原作者分享. class CTest{public: CTe ...

  4. php 基础语法

    <?php //注释 /* 多行注释 */ //输出语句 //echo "hello","helloa"; //print "world&quo ...

  5. SQL的IN, SOME,ANY,IN

    表dbo.Student有12条数据 name 123123123123123123123123123123123123大雄1阿华浩然菊花大姐123123 1.some,any用法一样(不知道有没有其 ...

  6. 会员制实现C2B定制有机农产品,被中粮我买投资的良食网这样卖有机生鲜

    前几天,中粮我买网战略投资了位于深圳的有机生鲜自营平台良食网,宣布双方将会在供应链上展开合作.然而良食网对大家来说还是比较陌生的,为此36氪专访了良食网的创始人唐忠. 良食网成立于2011年,是一家以 ...

  7. Java Hour 28 HashSet

    有句名言,叫做10000小时成为某一个领域的专家.姑且不辩论这句话是否正确,让我们到达10000小时的时候再回头来看吧. Hour 28 HashSet 为查找而生 LinkedList查找效率低下, ...

  8. hdu 1754 单点更新

    题意:很多学校流行一种比较的习惯.老师们很喜欢询问,从某某到某某当中,分数最高的是多少.这让很多学生很反感.不管你喜不喜欢,现在需要你做的是,就是按照老师的要求,写一个程序,模拟老师的询问.当然,老师 ...

  9. jsp放在web-inf下的注意事项

    转自:http://dejazhan.iteye.com/blog/1708785 web-inf目录是不对外开放的,外部没办法直接访问到(即不能通过URL访问).所有只能通过映射来访问,比如映射为一 ...

  10. select()函数以及FD_ZERO、FD_SET、FD_CLR、FD_ISSET(转)

    select函数用于在非阻塞中,当一个套接字或一组套接字有信号时通知你,系统提供select函数来实现多路复用输入/输出模型, 原型: int select(int maxfd,fd_set *rds ...