当前位置:网站首页>POJ 2392 Space Elevator

POJ 2392 Space Elevator

2022-07-07 19:09:00 Full stack programmer webmaster

Hello everyone , I meet you again , I'm the king of the whole stack .

Multiple knapsack problem .

My backpack training question 3 , Multiple backpack .

It seems that I understand multiple backpacks a little .

My understanding of backpack nine is about multiple backpacks :

When something Volume * Number When it exceeds the capacity of the backpack , This makes a complete backpack ( Equivalent to infinite access )

void completepack(int h,int cost,int a)
{
    for(int i=cost;i<=a;i++)
        dp[i]=max(dp[i],dp[i-cost]+h);
}

When there are more than one item but it can't fill the backpack , Do it one by one 01 knapsack Too wasteful . Then take the binary method , Every time I take 2.

Multiply every time 2 Of Do it once 01 knapsack .

In this way, the time complexity is reduced .

void zeroonepack(int h,int cost,int a)
{
    for(int i=a;i>=cost;i--)
        dp[i]=max(dp[i],dp[i-cost]+h);
}

Separate like this and then decompose . Making multiple backpacks easy .

void multiplepack(int h,int cost,int c,int a)
{
    if(cost*c>=a)
    {
        completepack(h,cost,a);
        return;
        // It's equivalent to making a complete backpack 
    }
    int k=1;
    while(k<c)
    {
        zeroonepack(h*k,cost*k,a);
        c-=k;
        k*=2;
        // many times 01 knapsack 
    }
    zeroonepack(h*c,cost*c,a);
}

AC Code :

#include<cstdio>
#include<cstring>
#include<string>
#include<queue>
#include<algorithm>
#include<queue>
#include<map>
#include<stack>
#include<iostream>
#include<list>
#include<set>
#include<cmath>
#define INF 0x7fffffff
#define eps 1e-6
#define LL long long
using namespace std;
int dp[40001];
int n;
struct lx
{
    int h,c,a;
}l[401];
bool cmp(lx a,lx b)
{
    return a.a<b.a;
}
void zeroonepack(int h,int cost,int a)
{
    for(int i=a;i>=cost;i--)
        dp[i]=max(dp[i],dp[i-cost]+h);
}
void completepack(int h,int cost,int a)
{
    for(int i=cost;i<=a;i++)
        dp[i]=max(dp[i],dp[i-cost]+h);
}
void multiplepack(int h,int cost,int c,int a)
{
    if(cost*c>=a)
    {
        completepack(h,cost,a);
        return;
    }
    int k=1;
    while(k<c)
    {
        zeroonepack(h*k,cost*k,a);
        c-=k;
        k*=2;
    }
    zeroonepack(h*c,cost*c,a);
}
int main()
{
    while(scanf("%d",&n)!=EOF)
    {
        int m=0;
        for(int i=0;i<n;i++)
        {
            scanf("%d%d%d",&l[i].h,&l[i].a,&l[i].c);
            m=max(m,l[i].a);
        }
        memset(dp,0,sizeof(dp));
        sort(l,l+n,cmp);
        for(int i=0;i<n;i++)
        {
            multiplepack(l[i].h,l[i].h,l[i].c,l[i].a);
        }
        int ans=0;
        for(int i=0;i<=m;i++)
            //printf("%d =\n",dp[i]);
            ans=max(ans,dp[i]);
        printf("%d\n",ans);

    }
}

Publisher : Full stack programmer stack length , Reprint please indicate the source :https://javaforall.cn/116608.html Link to the original text :https://javaforall.cn

原网站

版权声明
本文为[Full stack programmer webmaster]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/188/202207071653296850.html