poj 3628 Bookshelf 2 基本01背包

题目大意:FJ有n头奶牛,和一个高为h的架子,给出每头奶牛高度,求使奶牛叠加起来超过架子的最低高度是多少。

题目思路:求出奶牛叠加能达到的所有高度,并用dp[]保存,最后进行遍历,找出与h差最小的dp[]即所求答案。

#include<cstdio>
#include<stdio.h>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
#include<queue>
#define INF 0x3f3f3f3f
#define MAX 2000005

using namespace std;

int dp[MAX],a[MAX],sum,ans;

void Init()
{
    sum=0;
    ans=INF;
    memset(dp,0,sizeof(dp));
}
int main()
{
    int i,j,maxn,n,h;

    while(scanf("%d%d",&n,&h)!=EOF)
    {
        Init();

        for(i=1;i<=n;i++)
            {scanf("%d",&a[i]);sum+=a[i];}

        for(i=1;i<=n;i++)
        {
            for(j=sum;j>=a[i];j--)
            {
                dp[j]=max(dp[j],dp[j-a[i]]+a[i]);
            }
        }

        for(i=1;i<=sum;i++)
        {
            if(dp[i] >= h)//如果满足条件,则继续进行比较,保留较小的值
            {
                ans=min(ans,dp[i]-h);
            }
        }

        printf("%d\n",ans);
    }
    return 0;
}

时间: 2024-08-10 21:16:33

poj 3628 Bookshelf 2 基本01背包的相关文章

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

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 (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 题解

本题解法很多,因为给出的数据特殊性故此可以使用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 1837 Balance (dp,01背包)

链接:poj 1837 题意:有一个天平,天平左右两边各有若干个钩子,总共有C个钩子,有G个钩码, 求将钩码挂到钩子上使天平平衡的方法的总数.其中可以把天枰看做一个以x轴0点作为平衡点的横轴 分析:力臂=重量 *臂长 = g[i]*c[j] 当平衡度k=0时,说明天枰达到平衡,k>0,说明天枰倾向右边(x轴右半轴),k<0则左倾 因此可以定义一个 状态数组dp[i][k],意为在挂满前i个钩码时,平衡度为k的挂法的数量. 由于距离c[i]的范围是-15~15,钩码重量的范围是1~25,钩码数量

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

poj 2184 Cow Exhibition(01背包)

Cow Exhibition Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10882   Accepted: 4309 Description "Fat and docile, big and dumb, they look so stupid, they aren't much fun..." - Cows with Guns by Dana Lyons The cows want to prove to

poj(2184)——Cow Exhibition(01背包变形)

其实我想说这道题我觉得我自己并没有深刻的理解.但是今天做了一下,先把现在的想法记录下来 . 题目的大致意思是: 有N头牛,每头牛都有一个s值代表智商值,f值代表它的幽默值. 然后问你智商值和幽默值的总和值最大是多少,其中必须保证智商值的和与幽默值的和为非负数. 一开始我想到的也是01背包,但是这里还有负值,于是我就没有办法了.于是学习到了一个相当于把坐标平移的方法. 因为这里有-1000到0的值,于是我们把它们全都移到右边去,于是变成了非负值0-2000. 解法: 01背包. 但是要注意的是当x

poj 2923 状压dp+01背包

好牛b的思路 题意:一系列物品,用二辆车运送,求运送完所需的最小次数,两辆车必须一起走 解法为状态压缩DP+背包,本题的解题思路是先枚举选择若干个时的状态,总状态量为1<<n,判断这些状态集合里的那些物品能否一次就运走,如果能运走,那就把这个状态看成一个物品.预处理完能从枚举中找到tot个物品,再用这tol个物品中没有交集(也就是两个状态不能同时含有一个物品)的物品进行01背包,每个物品的体积是state[i],价值是1,求包含n个物品的最少价值也就是dp[(1<<n)-1](dp