[Bzoj 1192] [HNOI2006] Guiguzi purse (binary multiple optimization backpack)

( Life first post bzoj solution to a problem a little excited 

First, tell us about the topic:

        Title see it so long, in fact, means that a given number a, which is split into a required number n, can be expressed by this number n in Scheme 1 ~ a of all numbers, and seeking the minimum of n.

       You read the thing? I do not know to give chestnuts:

       3 may be changed to (1,2) the two numbers ( nonsense, of course I know ), so that the numbers 1, 2, 1, 2 can be shown. This question is to see the thought of long ago seen optimize multiple binary backpack. So alike.

       We think we have a number for P. We can be divided into two parts (1 ~ P / 2) and (P / 2 + 1, P ), we think: Suppose we already know (1 ~ P / 2) requires a minimum number m, then we can in Add a P / 2 the number of sections on the basis of the left, so that this may represent a number of m + 1 (1 ~ P) of all numbers, because (1 + P / 2 ~ P/ 2 + 1 ~ P).

       In fact, we can turn by hand + scratch paper found on a minimum value of P n is (1 ~ P) of the left half of the interval (1 ~ P / 2) +1 minimum, whereas (1 ~ P / 2) and the minimum value of the left half of its own range of relevant, so in fact this is a process can be recursive.

       Then we summarize what --P and P / 2 related to P / 2 and the P / 4 about, it is easy to think of the binary. We just after a somewhat extended operator hand we will find - on a number P, we make a number satisfying s 2 s  -1 <P, P less than strict attention, i.e., s is log2 (P), then the number of P ....... 2 can be 1, 2,4 S-. 1 , P-2 + 1'd decomposition.

      So in fact, we will soon be able to know the minimum number n a P corresponds, in fact, is log2 (P) +1. This question is so simple it up, paste the code

#include<iostream>
#include<cmath>
#include<cstdio> 
using namespace std;
int main()
{
    long long a;
    scanf("%lld",&a);
    printf("%lld",(long long )log2(a)+1);
}
Guiguzi purse

     In fact, this question has on luogu enhanced version https://www.luogu.org/problem/P2320, which requires the number of output for each purse, This question is also very simple, we first find the above reasoning by S , then in order to obtain ~ 2. 1 S. 1-  P-2 and S + 1'd can reorder the output ( reportedly was only count n, luogu this question is added, but the data looks like pot )

#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
long long p,s,k;
int main()
{
    scanf("%lld",&p);
    printf("%lld\n",(long long)log2(p)+1);
    s=log2(p);
    k=p-(1<<s)+1;
        for(long long i=0;i<=s-1;i++)
        {
            if(1<<i>k&&k!=0)
            {
                printf("%lld ",k);
                k=0;
            }
            printf("%lld ",1<<i);
        }
        if(k!=0)
        printf("%lld ",k);
    return 0;
 } 
Guiguzi purse (enhanced version

Next, a brief description of the title written by binary Optimized multi backpack

Our general idea is to enumerate the multiple backpack to take the same kind of n items into a backpack 01

Such that the time complexity of O (NV * Σ (V / W [i])). (V is the volume, W [i] is the cost per piece)

But if we are like items each purse Guiguzi as binary split that each article will be broken up into log2 (W [i]) + 1 th article

That so do 01 backpack The time complexity is O (V * log (W [ i]) + V) (PS: This time complexity is likely wrong , but should be similar)

After about binary Optimized multi backpack I will write a blog is concerned, this is the main problem solution hhh

You understand well, walking.

I do not understand that we can add to discuss Oh qq2733524923

Guess you like

Origin www.cnblogs.com/dixiao/p/11415941.html