POJ 3628 Bookshelf 2(01背包)
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背包)的更多相关文章
- POJ 3628 Bookshelf 2 0-1背包
传送门:http://poj.org/problem?id=3628 题目看了老半天,牛来叠罗汉- -|||和书架什么关系啊.. 大意是:一群牛来叠罗汉,求超过书架的最小高度. 0-1背包的问题,对于 ...
- POJ 3628 Bookshelf 2 (01背包)
Bookshelf 2 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7496 Accepted: 3451 Descr ...
- POJ 3628 Bookshelf2(0-1背包)
http://poj.org/problem?id=3628 题意:给出一个高度H和n个牛的高度,要求把牛堆叠起来达到H,求出该高度和H的最小差. 思路:首先我们计算出牛的总高度sum,sum-H就相 ...
- POJ 3628 Bookshelf 2【背包型DFS/选or不选】
Bookshelf 2 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11105 Accepted: 4928 Desc ...
- POJ 3211 Washing Clothes(01背包)
POJ 3211 Washing Clothes(01背包) http://poj.org/problem?id=3211 题意: 有m (1~10)种不同颜色的衣服总共n (1~100)件.Dear ...
- POJ 3628 Bookshelf 2【01背包】
题意:给出n头牛的身高,以及一个书架的高度,问怎样选取牛,使得它们的高的和超过书架的高度最小. 将背包容量转化为所有牛的身高之和,就可以用01背包来做=== #include<iostream& ...
- poj 3628 Bookshelf 2
http://poj.org/problem?id=3628 01背包 #include <cstdio> #include <iostream> #include <c ...
- POJ3628 Bookshelf 2(01背包+dfs)
Bookshelf 2 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8745 Accepted: 3974 Descr ...
- [POJ 2184]--Cow Exhibition(0-1背包变形)
题目链接:http://poj.org/problem?id=2184 Cow Exhibition Time Limit: 1000MS Memory Limit: 65536K Total S ...
- POJ 3624 Charm Bracelet(01背包裸题)
Charm Bracelet Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 38909 Accepted: 16862 ...
随机推荐
- js将map转成数组
//根据资源的ID去查找 this.classArray = []; for(var c in this.comboData.classId){ this.classArray.push({ text ...
- 51nod 1264 线段相交
题目:传送门. 题意:给两条线段,有一个公共点或有部分重合认为相交,问他们是否相交. 题解:这属于非规范相交的情况,模板题. #include <iostream> #include &l ...
- Myeclipse 安装svn插件
安装subclipse, SVN插件1.从官网下载site-1.8.22.zip文件 访问不了可点我网盘2.从中解压出features与 plugins文件夹,复制到MyEclipse\MyEcl ...
- sizeof(class)
//#define _REENTRANT //#define _POSIX_C_SOURCE #include <iostream> #include <string> #in ...
- OOP 7大原则
1. 开闭原则(Open-Closed Principle,OCP) 1)定义:一个软件实体应当对扩展开放,对修改关闭( Software entities should be open for e ...
- Telnet命令检测远程主机上的端口是否开启
ping命令不能检测端口,只能检测你和相应IP是否能连通. 本地虚拟机里安装了一个Ubuntu,使用Putty连接22端口操作时提示失败,于是查看对应端口是否开启. Windows下要检测远程主机上的 ...
- 以多个实例方式打开Notepad++
Right-click any Notepad++ shortcut. Select Properties. Move to the Shortcut tab. In the end of the T ...
- 如何在postgresql中模拟oracle的dual表,来测试数据库最基本的连接功能?
还好,网上弄到的,,没有dual的数据库,可以试图用select函数不带from数据表的方式来实现返回值. 一段测试代码: try: conn = psycopg2.connect(database= ...
- Sql server之路 (六)上传服务器图片
原理: 上传图片的名字 插入到数据库里 上传图片的内容(二进制数据) 写到服务器指定的目录下 下次读取图片的时候 从数据库里的指定字段里读取图片文件名 从数据库的指定路径下 拼串成完成的路径 就可以下 ...
- java程序员必须会的技能
1.语法:必须比较熟悉,在写代码的时候IDE的编辑器对某一行报错应该能够根据报错信息知道是什么样的语法错误并且知道任何修正. 2.命令:必须熟悉JDK带的一些常用命令及其常用选项,命令至少需要熟悉:a ...