poj 2392 Space Elevator (多重背包)

Space Elevator

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 8110   Accepted: 3843

题目大意  :一群牛要上天  用一些石块堆塔  给出石块的种类  及其每个种类的数量 和该种石块能出现的最高高度  和每种石块的数量

求怎么摆放才能堆得最高

多重背包模板题。。。。

将所有石块排序  把高度低的放下面

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
struct Class{
    int h,maxh,c;
}a[405];
int cmp(Class x,Class y)
{
    return x.maxh<y.maxh;
}
int main()
{
    int n,ans,i,j,dp[40005],sum[40005];
    while(cin>>n)
    {
        for( i=0;i<n;i++)
            cin>>a[i].h>>a[i].maxh>>a[i].c;

        sort(a,a+n,cmp);
        memset(dp,0,sizeof(dp));
        dp[0]=1;
        int ans=0;
        for(i=0;i<n;i++)
        {
            memset(sum,0,sizeof(sum));
            for(j=a[i].h;j<=a[i].maxh;j++)
            {
                if(!dp[j]&&dp[j-a[i].h]&&sum[j-a[i].h]<a[i].c)
                {
                    dp[j]=1;
                sum[j]=sum[j-a[i].h]+1;//统计石块放置的个数
                if(ans<j)
                ans=j;

                }
            }
        }
        cout<<ans<<endl;

    }

    return 0;
}
时间: 2024-08-03 07:26:38

poj 2392 Space Elevator (多重背包)的相关文章

POJ 2392 Space Elevator(多重背包)

Description The cows are going to space! They plan to achieve orbit by building a sort of space elevator: a giant tower of blocks. They have K (1 <= K <= 400) different types of blocks with which to build the tower. Each block of type i has height h

poj 2392 Space Elevator(多重背包+先排序)

Description The cows are going to space! They plan to achieve orbit by building a sort of space elevator: a giant tower of blocks. They have K (1 <= K <= 400) different types of blocks with which to build the tower. Each block of type i has height h

POJ 2392 Space Elevator(贪心+多重背包)

POJ 2392 Space Elevator(贪心+多重背包) http://poj.org/problem?id=2392 题意: 题意:给定n种积木,每种积木都有一个高度h[i],一个数量num[i],还有一个限制条件,这个积木所在的位置不能高于limit[i],问能叠起的最大高度? 分析: 本题是一道多重背包问题, 不过每个物品的选择不仅仅要受该种物品的数量num[i]限制, 且该物品还受到limit[i]的限制. 这里有一个贪心的结论: 我们每次背包选取物品时都应该优先放置当前limi

POJ 2392 Space Elevator 背包题解

多重背包,本题不需要二分优化.相对简单点.因为重复数十分小,小于10: 而增加一个限制每种材料的高度做法,如果使用逆向填表,那么只需要从这个高度往小递归填表就可以了. 还有就是注意要排序,以限制高度为标准从小到大排序,否则答案错误的. #include <stdio.h> #include <string.h> #include <algorithm> using std::sort; const int MAX_K = 401; const int MAX_H = 4

poj 2392 Space Elevator DP

该题与poj 1742的思路基本一致:http://www.cnblogs.com/sevenun/p/5442279.html(多重背包) 题意:给你n个电梯,第i个电梯高h[i],数量有c[i]个,但是每个电梯所在高度不能超过a[i]. 求问,怎么样的建造方案能够使电梯能够达到最大高度 思路:首先,必然要使电梯按a[i]进行排序,a[i]最小的电梯先建造.例如,电梯1,只能在高度20以下建造,而电梯2能在高度50以下建造,我当然先建造电梯1,否则如果先建造电梯2,就会导致我建造的高度早早超过

poj 2392 (Space Elevator) 1276 (Cash Machine)变形背包

这道题跟coins很像,看来楼教主的男人八题果然不简单. 进行coins式的背包处理就好了. 2392 #include<iostream> #include<algorithm> #include<string.h> #include<stdlib.h> #include<stdio.h> #define max(a,b) ((a)>(b)?(a):(b)) typedef long long ll; using namespace st

POJ 2392 Space Elevator 贪心+dp

题目链接: http://poj.org/problem?id=2392 题意: 给你k类方块,每类方块ci个,每类方块的高度为hi,现在要报所有的方块叠在一起,每类方块的任何一个部分都不能出现在ai以上的高度,问这些方块能叠的最高高度. 题解: 首先按ai升序排序,尽量让高度限制低的先排掉,如果不这样做一些转移会失效掉: 比如:h1=3,a1=3;h2=4,a2=7 如果先搭1再搭2则合法,但反过来则变成无效的转移了. 处理好顺序之后跑一遍背包就可以了,因为最大高度为40000,比较小,所以用

POJ 2392 Space Elevator

排序+背包. 先对按高度限制从小到大排序,然后做背包即可.0/1背包300多ms过的,可以用完全背包二进制优化. #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> using namespace std; const int maxn=400+10; int n; struct X { int h,c,a; }s[maxn]; int dp[400000+10]; i

poj 1276 Cash Machine (多重背包)

链接:poj 1276 题意:已知金额cash,给定几种不同面值的货币的数量及面值,求利用给定的货币可以凑成 小于等于cash的金额的最大值 分析:因为每种货币的面值及数量已知,可以将其转化为多重背包,背包的容量即为cash, 每个物品的价值及费用都为每种货币的面值. 多重背包可以转化为01背包,不过这样会超时,为了避免这样,可以转化为完全背包和二进制思想的01背包 #include<stdio.h> #include<string.h> int f[100010],v; int