POJ 3628 Bookshelf 2【01背包】

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

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

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 #define maxn 2000005
 6 using namespace std;
 7 int dp[maxn],h[50];
 8 int main()
 9 {
10     int n,b,i,j,sum=0,tmp=1000010;
11     scanf("%d %d",&n,&b);
12     for(i=1;i<=n;i++)
13     {
14         scanf("%d",&h[i]);
15         sum+=h[i];
16     }
17     for(i=1;i<=n;i++)
18     {
19         for(j=sum;j>=h[i];j--)
20         {
21             dp[j]=max(dp[j],dp[j-h[i]]+h[i]);
22             {
23                 if(dp[j]>=b&&j<tmp)
24                 tmp=j;
25             }
26         }
27     }
28     printf("%d\n",tmp-b);
29 }

时间: 2024-10-18 20:11:40

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

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

POJ 3628 Bookshelf 2 0/1背包和DFS两种解法

题目链接:POJ 3628 Bookshelf 2 Bookshelf 2 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7462   Accepted: 3436 Description Farmer John recently bought another bookshelf for the cow library, but the shelf is getting filled up quite quickly,

POJ 3628 Bookshelf 2 题解

本题解法很多,因为给出的数据特殊性故此可以使用DFS和BFS,也可以使用01背包DP思想来解. 因为一般大家都使用DFS,这里使用很少人使用的BFS,缺点是比DFS更加耗内存,不过优点是速度比DFS快. 当然也比DFS难写点: int N, B; int Height[21]; inline int mMin(int a, int b) { return a > b? b : a; } inline int mMax(int a, int b) { return a < b? b : a; }

POJ 3624 Charm Bracelet(01背包裸题)

Charm Bracelet Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 38909   Accepted: 16862 Description Bessie has gone to the mall's jewelry store and spies a charm bracelet. Of course, she'd like to fill it with the best charms possible fro

poj 3211 Washing Clothes 0-1背包

题意: 有2个人洗n件衣服,每件衣服有需要洗的时间和颜色,只能相同颜色的衣服两人一起洗,求洗完衣服的最少时间. 分析: 0-1背包判断某个重量是否能达到. 代码: //poj 3211 //sep9 #include <iostream> #include <map> #include <string> using namespace std; const int maxN=128; int m,n; map<string,int> name; int v[

poj 3628 Bookshelf 2

Bookshelf 2 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10981   Accepted: 4860 Description Farmer John recently bought another bookshelf for the cow library, but the shelf is getting filled up quite quickly, and now the only availabl

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

POJ 3211 Washing Clothes(01背包)

http://poj.org/problem?id=3211 题意: 有m (1~10)种不同颜色的衣服总共n (1~100)件,Dearboy和她的girlfriend两个人要一起洗完全部衣服,为了预防色彩混合,他们每次只能同时洗同一种颜色的衣服,给出洗完每件衣服所需的时间time和它的颜色color,求出Dearboy和她的girlfriend最少用多少时间能洗完成全部衣服. 分析: 由于每种颜色的衣服是分开洗的, 所以我们可以把所有衣服按颜色分类, 然后每次看洗一种颜色的衣服最少需要花多少

POJ 2184 Cow Exhibition (01背包)

题意:每行给出si和fi,代表牛的两个属性,然后要求选出几头牛,是的则求出总S与总F的和,注意S与F都不能为负数 析:用dp[i]来表示存放s[i]的时最大的f[i],其实就是一个01背包.只是取不取的关系.注意是有负数,所以把数组开大一点,然后s[i]的正负数, 我们取的顺序不同,正数是逆向,负数是正向,要不然可能有重复.还不知道为什么交G++就RE,交C++就能过. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000&quo