Backpack-binary Optimized

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int n,m,ans,cnt=1;
int f[1000005];
int w[1000005],v[1000005];//记得将数组开大
int main()
{
    int a,b,c;
    scanf("%d%d",&n,&m);

    for(int i=1;i<=n;++i)
    {
        scanf("%d%d%d", & A, & B, & C);
         for ( int J = . 1 ; J <= C; J << = . 1 ) 
        { 
            V [ ++ CNT] = J * A, W [CNT] = J * B; 
            C - = J; 
        } 
        IF (C) V [CNT ++] = A * C, W [CNT] * B = C;
         // binary optimization, split 
    } 

    for ( int I = . 1 ; I <= CNT; ++ I)
      for ( int J = m; J> = W [I]; - J) 
      F [J] = max (F [J], F [JW [I]] + V [I]);
     // simple multiple backpack 
    printf ( "%d\n",f[m]);
    return 0;
}

It used here is binary Optimized

First of all we need to know the principles of binary optimization

To 19, for example, if we split into 1,2,4,8,3

We can use the number five shows before all numbers from 1 to 19

Then the number five is how come it

In fact, adding a power of less than or equal (1,2,4,8) and the number 2 and that the difference between the number of (3)

If we optimized the binary for each item you can increase the space complexity and reduce the time complexity

Topic: https: //www.luogu.org/problemnew/show/P1776

Excerpt from: https: //dankuroto.blog.luogu.org/solution-p1776

Guess you like

Origin www.cnblogs.com/kingderman/p/11232242.html