【Manthan, Codefest 18 (rated, Div. 1 + Div. 2) A】Packets

【链接】 我是链接,点我呀:)
【题意】

在这里输入题意

【题解】

多重背包的二进制优化。 就是将数量x分成接近log2x份 然后这log2x份能组合成1..x内的所有数字。 从而将多重背包转化成01背包 1,2,4,8....贪心地选,然后不够的部分x-(1+2+4...)再作为一份就好

【代码】

#include <bits/stdc++.h>
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define all(x) x.begin(),x.end()
#define pb push_back
#define lson l,mid,rt<<1
#define ri(x) scanf("%d",&x)
#define rl(x) scanf("%lld",&x)
#define rs(x) scanf("%s",x)
#define rson mid+1,r,rt<<1|1
using namespace std;

const double pi = acos(-1);
const int dx[4] = {0,0,1,-1};
const int dy[4] = {1,-1,0,0};

int n;

int main(){
	#ifdef LOCAL_DEFINE
	    freopen("rush_in.txt", "r", stdin);
	#endif
    scanf("%d",&n);
    int now = 0,cnt = 0;
    for (int i = 1; ;i*=2){
        now+=i;
        if (now>n) {
            now-=i;
            break;
        }
        cnt++;
    }
    if (now<n) cnt++;
    printf("%d
",cnt);
	return 0;
}
原文地址:https://www.cnblogs.com/AWCXV/p/9576635.html