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_i (1 <= h_i <= 100) and is available in quantity c_i (1 <= c_i <= 10). Due to possible damage caused by cosmic rays, no part of a block of type i can exceed a maximum altitude a_i (1 <= a_i <= 40000). 

Help the cows build the tallest space elevator possible by stacking blocks on top of each other according to the rules.

Input

* Line 1: A single integer, K 

* Lines 2..K+1: Each line contains three space-separated integers: h_i, a_i, and c_i. Line i+1 describes block type i.

Output

* Line 1: A single integer H, the maximum height of a tower that can be built

Sample Input

37 40 35 23 82 52 6

Sample Output

48

Hint

OUTPUT DETAILS: 

From the bottom: 3 blocks of type 2, below 3 of type 1, below 6 of type 3. Stacking 4 blocks of type 2 and 3 of type 1 is not legal, since the top of the last type 1 block would exceed height 40.

题目大意:有一头奶牛要上太空,他有很多种石头,每种石头的高度是hi,但是不能放到ai之上的高度,并且这种石头有ci个
将这些石头叠加起来,问能够达到的最高高度。  
解题思路:
首先对数据进行升序排序,这样才是一个标准的多重背包的问题
为什么要排序?
因为只有这样才能得到最优解,如果一开始就是高的在前面,那么后面有低的却不能选到,就直接选高的去了。这样是不能达到最优解的
使f[i]的状态标记,是否可以达到这个高度
这样能够达到取f[i]中i的最大值即可。
这里要注意max赋初值的时候要赋值为0,不能为-1,因为答案有可能为0  
做的时候不知道看数据的大小,以为对它进行二进制优化转为01背包会快一点,结果优化之后时间跟空间都变大了,而且把自己给搞糊涂了,每个物体的所在最大高度限制不知道怎么处理了,高了很长一段时间才给想明白,真是自己给自己找事。二进制转化之后的空间为
12324 KB,时间为313ms,网上大神写的简单代码空间只要484kb,时间只要63ms。
自己写的长长的代码: 弄巧成拙
#include<iostream>
#include<stdio.h>
#include<string.h>
#include <algorithm>
#define MAX 1000000
using namespace std;
int n,m;
int h[405],c[405],w[405],a[405];//a为价值,w为花费,c为数量 h为高度
struct block
{
    int h,c,w,a;
} num[405];
int Count;
int value[MAX];
int size[MAX];
int height[MAX];
int dp[100005];
bool cmp(block a,block b)
{
    if(a.h<b.h)
        return true;
    return false;
}
int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        Count=1;
        memset(value,0,sizeof(value));
        memset(size,0,sizeof(size));
        memset(height,0,sizeof(height));
        int mheight=0;
        for(int i=1; i<=n; i++)
        {
            scanf("%d%d%d",&num[i].a,&num[i].h,&num[i].c);
            if(num[i].h>mheight)
                mheight=num[i].h;
        }
        sort(num+1,num+n+1,cmp);
        for(int i=1; i<=n; i++)
        {
            for (int k=1; k<=num[i].c; k<<=1)
            {
                value[Count] = k*num[i].a;
                height[Count]=num[i].h;
                size[Count++] = k*num[i].a;
                num[i].c -= k;
            }
            if (num[i].c>0)
            {
                value[Count] = num[i].c*num[i].a;
                height[Count]=num[i].h;
                size[Count++] = num[i].c*num[i].a;
            }
        }
        memset(dp,0,sizeof(dp));
        for(int i=1; i<=Count; i++)
            for(int j=mheight; j>=size[i]; j--)
                if(j<=height[i])
                    dp[j]=max(dp[j],dp[j-size[i]]+value[i]);
        int Max=0;
        for(int k=0;k<=mheight; k++)
        {
            if(dp[k]>Max)
            {
                Max=dp[k];
            }
        }
        printf("%d\n",Max);
    }
    return 0;
}
大家就当看多重背包的二进制优化吧
网上大神的简易代码
#include <iostream>
#include <algorithm>
using namespace std;
#define MAXV 410
#define MAXM 40010

typedef struct {
	int h,a,c;
}Blocks;

Blocks v[MAXV];

int cmp(Blocks x,Blocks y){
	return x.a<y.a;
}
int f[MAXM],user[MAXM];

int main(){
	int i,t,j,max;
	while(~scanf("%d",&t)){
		for(i=1;i<=t;i++){
			scanf("%d%d%d",&v[i].h,&v[i].a,&v[i].c);
		}
		sort(v+1,v+t+1,cmp);
		memset(f,0,sizeof(f));
		f[0]=1;
		max=0;	//赋值为0,不能为-1
		for(i=1;i<=t;i++){
			memset(user,0,sizeof(user));
			for (j=v[i].h;j<=v[i].a;j++){
				if(!f[j] && f[j-v[i].h] && user[j-v[i].h]+1<=v[i].c){
					f[j]=1;
					user[j]=user[j-v[i].h]+1;//用于记录该石头用过的个数
					if(j>max) max=j;
				}
			}
		}
		printf("%d\n",max);
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值