poj2392 Space Elevator

http://poj.org/problem?id=2392

1.多重背包思路

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=40005;
int dp[N+1];
struct blocks {
    int h,a,num;
    bool operator < (const blocks &m)const {
        return this->a < m.a;
    }
} b[405];
int main() {
    int n,sum=0;
    scanf("%d",&n);
    for(int i=0; i<n; i++)
        scanf("%d%d%d",&b[i].h,&b[i].a,&b[i].num);
    sort(b,b+n);
    int i,j,k;
    memset(dp,0,sizeof(dp));
    for(i=0; i<n; i++) {
        int cnt=b[i].num;
        for(k=1; cnt>0; k<<=1) {
            int mul=min(k,cnt);
            for(j=b[i].a; j>=mul*b[i].h; j--) {
                dp[j]=max(dp[j],dp[j-mul*b[i].h]+mul*b[i].h);
            }
            cnt-=mul;
        }
    }
    int ans=0;
    for(i=0; i<=b[n-1].a; i++)
        ans=max(ans,dp[i]);
    printf("%d\n",ans);
    return 0;
}


2.完全背包思路

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespacestd;
const int N=40005;
int dp[N+1];
struct blocks {
    int h,a,num;
    bool operator < (const blocks &m)const {
        return this->a < m.a;
    }
} b[405];
int main() {
    int n,sum=0;
    scanf("%d",&n);
    for(int i=0; i<n; i++)
        scanf("%d%d%d",&b[i].h,&b[i].a,&b[i].num);
    sort(b,b+n);
    int i,j,k;
    memset(dp,0,sizeof(dp));
    for(i=0; i<n; i++) {
        for(k=1; k<=b[i].num; k++) {
            for(j=b[i].a; j>=b[i].h; j--) {
                dp[j]=max(dp[j],dp[j-b[i].h]+b[i].h);
            }
        }
    }
    int ans=0;
    for(i=0; i<=b[n-1].a; i++)
        ans=max(ans,dp[i]);
    printf("%d\n",ans);
    return 0;
}


实际上完全背包问题和多重背包问题都可以由01背包思想变化而来

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值