poj2392 Space Elevator

来源:互联网 发布:oracle drop数据恢复 编辑:程序博客网 时间:2024/05/22 15:36

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背包思想变化而来