POJ3628 Bookshelf 2(01背包+dfs)

Bookshelf 2

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 8745   Accepted: 3974

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

题意: 求N个数中和比减16最小的。5+6+3+3 - 16 = 1最小的是1;01背包,背包的容量是所有的数累加起来的和sum,想想为什么呢,可以累加起来的和一定比给的那个数大,然后就从b-sum查找背包中的比b大的第一个数就ok了,很棒的转换dfs也能轻松搞定

 1 #include <iostream>
 2 #include <cstring>
 3 #include <algorithm>
 4 #include <cstdio>
 5 using namespace std;
 6 const int INF = 0x3f3f3f3f;
 7 const int MAX = 2000000 + 10;
 8 int dp[MAX],sum,a[MAX];
 9
10 int main()
11 {
12     int n,b;
13     while(scanf("%d%d", &n,&b) != EOF)
14     {
15         for(int i = 1; i <= n; i++)
16         {
17             scanf("%d", &a[i]);
18             sum += a[i];
19         }
20         memset(dp, 0, sizeof(dp));
21         for(int i = 1; i <= n; i ++)
22         {
23             for(int j = sum; j >= a[i]; j--)
24             {
25                 dp[j] = max(dp[j],dp[j - a[i]] + a[i]);
26             }
27         }
28         for(int i = b; i <= sum; i++)
29         {
30             if(dp[i] >= b)
31             {
32                 printf("%d\n",dp[i] - b);
33                 break;
34             }
35         }
36
37     }
38
39     return 0;
40 }

时间: 2024-07-30 10:06:13

POJ3628 Bookshelf 2(01背包+dfs)的相关文章

POJ 3628 Bookshelf 2 (01背包)

Bookshelf 2 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7496   Accepted: 3451 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

[Swust OJ 465]--吴奶奶买鱼(0-1背包+dfs)

题目链接:http://acm.swust.edu.cn/problem/465/ Time limit(ms): 1000 Memory limit(kb): 65535 Description 吴奶奶有个可爱的外孙女——琪琪,她很喜欢小动物,尤其喜欢养鱼.为了让小孙女养到漂亮的小鱼,吴奶奶一大早就到花鸟鱼虫市场买鱼.这个市场可真大,里面有各种各样的宠物,就连宠物鱼都有好几十种.这些鱼实在是太美了,买的人越来越多,可是因为货源有限,卖鱼的老板不得不规定:同一种鱼,每个人最多只能买一条,并且有些

Bookshelf 2 poj3628(01背包/DFS)

http://poj.org/problem?id=3628 题意:现有一个书架,N头牛,农场主想把这些牛放在书架上,当然,书架是有固定的高度的.现在问你在这些牛之中,其中一些牛的高度加在一块比书架高的最小差值是多少? 分析: 两种方法:(1)01背包,算得所有牛的高度,然后dp一遍,再找比书架高的最小值,两数相减即可.           (2)DFS,DFS(牛的坐标, 牛高度的总和),若在DFS过程中,牛高度的总和>=书架高度,则可以求我们所需要的值 01背包: #include<std

hdu3448 01背包+dfs

题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=3448 Description 0/1 bag problem should sound familiar to everybody. Every earth man knows it well. Here is a mutant: given the capacity of a bag, that is to say, the number of goods the bag can ca

HDU_2079_(01背包)(dfs)

选课时间(题目已修改,注意读题) Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4478    Accepted Submission(s): 3480 Problem Description 又到了选课的时间了,xhd看着选课表发呆,为了想让下一学期好过点,他想知道学n个学分共有多少组合.你来帮帮他吧.(xhd认为一样学分的课没区别)

codeforces 842C Ilya And The Tree (01背包+dfs)

(点击此处查看原题) 题目分析 题意:在一个树中,有n个结点,记为 1~n ,其中根结点编号为1,每个结点都有一个值val[i],问从根结点到各个结点的路径中所有结点的值的gcd(最大公约数)最大是多少,其中,我们可以将路径中某一个结点的值变为0,也可以选择不变. 思路:注意到对于每个结点,我们可以选择这个结点,或者不选这个结点(将权值记为0),因而有点01背包的感觉,而我们求gcd的时候需要取所有情况中的最大值 那么我们从根结点开始,每经过一个结点,就从其父节点的所有情况转移得到当前结点的状态

Bookshelf 2 01背包

B - Bookshelf 2 Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu Submit Status Description Farmer John recently bought another bookshelf for the cow library, but the shelf is getting filled up quite quickly, and now the only

UVA 624 CD (01背包+打印路径 或 dfs+记录路径)

Description You have a long drive by car ahead. You have a tape recorder, but unfortunately your best music is on CDs. You need to have it on tapes so the problem to solve is: you have a tape N minutes long. How to choose tracks from CD to get most o

hdoj 1864 最大报销额 【01背包】||【dfs】

最大报销额 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 17014    Accepted Submission(s): 4959 Problem Description 现有一笔经费可以报销一定额度的发票.允许报销的发票类型包括买图书(A类).文具(B类).差旅(C类),要求每张发票的总额不得超过1000元,每张发票上,单项物品的