Oversized Complete Backpack

We can do ordinary complete backpacks, but what if the capacity of the backpack is too large and the dp array cannot be opened? Here's a copy of the oversized complete backpack template.

Raise the Shanglan Empire

Time limit:  C/C++ 20000ms; Java 40000ms  Memory limit:  65535KB

Passes:  9Total  Submissions:  20

Problem Description

       Due to the daily affairs of zbt, the Shanglan Empire has already been turned upside down. So he was quickly included in the key education target of Shanglan Empire by SHlong. As an untamed person, zbt of course had to fight back, so he began to destroy everywhere. SHlong could not subdue zbt due to his limited ability, so he had to go to the ACM laboratory. Abbot Wukan was invited. Abbot Wukan said to zbt: "I heard that you are very good at the legendary backpack problem, then I will give you a complete backpack. If you can answer it, I will let you do whatever you want. If you can't answer, then I'll kick you out of the Shanglan Empire." Of course zbt agreed casually, so Abbot Wukan began to ask questions.

       There is a backpack of capacity S, and N items, each with a volume and value, and each item can be used indefinitely. Find the maximum value that can be put into the backpack if the capacity does not exceed S.

       Zbt looked at the question below and mocked: "Isn't this a big problem?" Before he finished speaking, he suddenly realized that there seemed to be some small (big) problems. So he secretly used the technique of sound transmission to ask you to help him solve this problem.


enter description

Input contains multiple sets of data

The first row of each set of data has two positive integers N and S, which represent the type of item and the capacity of the backpack, respectively.

The next N lines each have two numbers v, w represents the volume and value of each item.

1<=N,v<=1000;

0<=S,w<=1000000000;


output description

For each set of data, output a row, each row has only one positive integer, representing the largest value that can be put in.


sample input
3 100
14 5
13 2
5 1
Sample output
35
来源
第三届山西省大学生程序设计大赛

#include <iostream>
#include <cstdio>
#include <cstring>
#define me(x,y) memset(x,y,sizeof(x))
#define sd(x) scanf("%d",&x)
#define ss(x) scanf("%s",x)
#define sf(x) scanf("%f",&x)
#define slf(x) scanf("%lf",&x)
#define slld(x) scanf("%lld",&x)
#define pd(x) printf("%d\n",x)
#define plld(x) printf("%lld\n",x)
#define ps(x) printf("%s\n",x)
#define max(x,y) (x>=y?x:y)
#define min(x,y) (x<y?x:y)
#define sum(x,y) (x+y)
#define INF 0x3f3f3f3f

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn=1005;

ll n,c;
ll dp[maxn*5];
ll dp2[55][maxn*5];
ll w[maxn],v[maxn];
ll low[50];

void init() {
    me(dp,0);
    me(dp2,0);
    me(low,0);
}

int main() {
    while(scanf("%lld%lld",&n,&c)!=EOF) {
        ll maxv=0;
        for(int i=1;i<=n;i++) {
            slld(v[i]);
            slld(w[i]);
            maxv=max(maxv,v[i]);
        }
        init();
        ll s=c;
        ll cnt=0;
        while(s>0){
            low[++cnt]=s;
            s=((s-maxv)>>1);
        }
        for(ll i=1;i<=n;i++)
            for(ll j=v[i];j<=maxv*4;j++)
                dp[j]=max(dp[j],dp[j-v[i]]+w[i]);

        for(ll i=cnt;i>=1;i--){
            for(ll j=low[i];j<=low[i]+maxv*2;j++){
                if(j<=4*maxv) dp2[i][j-low[i]]=dp[j];
                else{
                    for(ll k=(j-maxv)/2;k<=j/2;k++)            //下一层
                        dp2[i][j-low[i]]=max(dp2[i][j-low[i]],dp2[i+1][k-low[i+1]]+dp2[i+1][j-k-low[i+1]]);
                }
            }
        }
        plld(dp2[1][0]);
    }
	return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325345333&siteId=291194637