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. 手把手教你cuda5.5与VS2010的编译环境搭建

    参考:http://www.cnblogs.com/xing901022/archive/2013/08/09/3248469.html 目前版本的cuda是很方便的,它的一个安装里面包括了Toolk ...

  2. eclipse 优化提速

    1.windows–>perferences–>general–>startup and shutdown关掉没用的启动项: WTP :一个跟myeclipse差不多的东西,主要差别 ...

  3. scp 指定端口

    scp -P33033 zp.tar root@111.222.123.01:/da1/web/zhaopin.shouhuobao.com #sshd的端口指定的是33033

  4. mysql 主主复制(双主复制)+ 配置KEEPALIVED实现热备

    binlog-do-db和replicate-do-db表示需要同步的数据库 binlog-ignore-db和replicate-ignore-db表示不需要同步的数据库 云端服务器为master配 ...

  5. ***PHP类型转换实例:$this->input->get()返回的结果是字符串类型(数字字符串转数字)

    一个GET的REST API,请求有三个参数,且都是数字 用$this->input->get()方法获取后的结果是字符串,需要转换为数字:如果没有获取到数据,则会返回false,需要用e ...

  6. centos7下安装vsftpd配置

    0. 首先安装ftp服务 yum install -y ftp 1. 通过yum install -y vsftp安装vsftp 2.    修改vi /etc/vsftpd/vsftpd.conf, ...

  7. Ubuntu小点汇总,更新中...

    转自:http://blog.csdn.net/zxz_tsgx/article/details/39713627 昨天重装了Ubuntu14.04 64位版,又被一些基础操作/设置给搞怕了,以前安装 ...

  8. php升级5.3到5.4,5.5,5.6

    Laravel要求php大于5.5.9,升级php5.3.3到5.6.14(最新版为 5.6.15 ) Add EPEL and Remi repositories onto your system: ...

  9. mysql的统计函数

    一:统计函数 MySQL提供5个统计函数来对对数据进行统计.分别是实现对记录进行统计数,计算和,计算平均数,计算最大值和计算最小值. 1. 统计数据记录条数 可以有两种方式: COUNT(*)使用方式 ...

  10. Windows内核下操作字符串!

    * Windows内核下操作字符串! */ #include <ntddk.h> #include <ntstrsafe.h> #define BUFFER_SIZE 1024 ...