[Jizhong analog 2019.08.18] [] completely backpack JZOJ6309

Topic Link

 

Meaning of the questions:

  N-$ $ There have an unlimited number of kinds of goods, as the bag space $ m $, $ I $ volume of the kinds of goods for $ $ a_i, value of $ b_i $. Seeking maximum value without exceeding backpack capacity achieved.

  $ N \ le 10 ^ 6, \ quad m \ le 10 ^ {16}, \ quad a_i, b_i \ le 100 $ and are positive integers.

 

analysis:

  A greedy: in fact, up to $ 100 \ times 100 = 10 ^ 4 $ kinds of items that can be considered de-duplication, so that $ n $ scale down to $ 10 ^ 4 $.

  Greedy II: If the volumes of the two items are the same, we certainly will not install the kind of lower value goods, such $ n $ scale down to $ 100 $.

  Transformation: Because $ m $ is too great, however, is small compared to $ a_i $, we can think of, the best cost items will appear very many times, so I am sure the final plan can be split into many small identical program (all equipment ), and then the rest of the budget capacity in the same article. So we do first backpack small capacity (maximum supported range), and again the composition of the final plan.

  In summary, we do first $ n \ le 100 $, $ m \ le 2 \ times 10 ^ 5 $ full backpack, the backpack provided $ F_i capacity represented $ $ i (1 \ le i \ le 2 \ times 10 ^ 5) the maximum value that can be acquired $, then there $$ f_m = \ mathop {max} \ limits_ {i = 1} ^ {2 \ times 10 ^ 5} \ {\ lfloor m / i \ rfloor \ times f_i + f_ {m \% i} \} $$

 

Implementation (100):

 

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define IL inline
using namespace std;
typedef long long LL;
const int N=100;
const int M=2e5;

    int n;
    LL m,w[103];
    LL f[M+3];

int main(){
    freopen("backpack.in","r",stdin);
    freopen("backpack.out","w",stdout);

    scanf("%d%lld",&n,&m);
    memset(w,0,sizeof w);
    for(int i=1;i<=n;i++){
        LL x,y;
        scanf("%lld%lld",&x,&y);
        w[x]=max(w[x],y);
        
    }
    
    memset(f,0,sizeof f);
    for(int i=1;i<=N;i++)
        for(int j=i;j<=M;j++)
            f[j]=max(f[j],f[j-i]+w[i]);
    
    LL ans=0;
    for(int i=1;i<=M;i++)
        ans=max(ans,m/i*f[i]+f[m%i]);
    
    printf("%lld",ans);

    return 0;

}
View Code

 

summary:

  Different data scale, creating different topics. You should learn to be flexible.

Guess you like

Origin www.cnblogs.com/Hansue/p/11374000.html