D. Fill The Bag

Topic links: http://codeforces.com/contest/1303/problem/D

Question is intended: to give you a bag, a box size of n and m, the size of each box is a [i], and a [i] is a positive integer power of 2, each box size can be split into two equal box, after several split asked to just fill the bag at least m, -1 is output if not impossible.

Thinking of: m into two 229 b stored in the array, since a [i] is a positive integer power of 2, that the statistical power of 2 corresponding to the array by the number of c, respectively, and then traverse from a low to a binary m high, if there is a corresponding array c, then c [i] -, otherwise see c [1] to c [i-1] can be summarized into whether or not, then we should be summarized those low, then you can not Get a high split, and the split number of times can be accumulated.

#include<stdio.h>
#include<iostream>
#include<string.h>
#include<math.h>
#include<string>
#include<algorithm>
#include<queue>
#include<map>
typedef long long ll;
using namespace std;
ll lo(ll x)
{
    ll v=0;
    while(x)
    {
        v++;
        x/=2;
    }
    return v;
}
ll b[100],c[100];
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        memset(b,0,sizeof(b));
        memset(c,0,sizeof(c));
        ll m,n;
        scanf("%lld%lld",&m,&n);
        ll sum=0;
        for(int i=1;i<=n;i++)
        {
            ll x;
            scanf("%lld",&x);
            sum+=x;
            c[lo(x)]++;
        }
        if(sum<m)
        {
            printf("-1\n");
            continue;
        }
        int x=0;
        while(m)
        {
            x++;
            b[x]=m%2;
            m/=2;
        }
        ll ans=0;
        for(int i=1;i<=65;i++)
        {
            if(b[i]==0)
                continue;
            if(c[i]>0)
                c[i]--;
            else
            {
                ll y=2,z=-1;
                for(int j=i-1; J> = . 1 ; J,) //See whether may be summarized as I, 
                {
                     IF (C [J]> = Y) 
                    { 
                        C [J] - = Y; 
                        Z = J + . 1 ;
                         BREAK ; 
                    } 
                    the else 
                        Y - = C [J]; 
                    Y * = 2 ; 
                } 
                IF (! Z = - . 1 ) // can subtract the corresponding 
                {
                     for ( )int J = Z; J <I; J ++
                        C [J] = 0 ; 
                } 
                the else 
                { 
                    for ( int J = I + . 1 ; J <= 65 ; J ++) // not find the high resolution 
                    {
                         IF (C [J]> 0 ) 
                        { 
                            C [J] - - ;
                             for ( int K = J- . 1 ; K> = I; K-- ) 
                            { 
                                C [K] ++ ;
                                ans++;
                            }
                            break;
                        }
                    }
                }
            }
        }
        printf("%lld\n",ans);
    }
}

 

Guess you like

Origin www.cnblogs.com/zcb123456789/p/12302062.html
Bag
Recommended