zoukankan      html  css  js  c++  java
  • Codeforces1303D. Fill The Bag

    1e18对应2进制有58位,可以直接暴力模拟,因为读入的数都是2次幂,__builtin_ctz这个内置gcc函数可以算出二进制下末尾有几个0,读入时统计,然后从n的最低位开始判断,注意每次升位的时候如果有2个相同的就要向上一位进位,当拆掉比他大的数时,必定对比他大的位各个产生一个1,因为>>1

    #include<bits/stdc++.h>
    using namespace std;
    #define lowbit(x) ((x)&(-x))
    typedef long long LL;
    
    int bit[65];
    LL n;
    
    int solve() {
        int ans = 0;
        for(int i = 0; i < 60; ++i) {
            if(n&(1LL<<i)) {
                for(int j = i; j <= 60; ++j) {
                    if(j == 60) return -1;
                    if(bit[j]) {
                        bit[j]--;
                        for(int k = i; k < j; ++k)
                            bit[k]++;
                        break;
                    }
                    ans++;
                }
            }
            bit[i+1] += bit[i] /2;
        }
        return ans;
    }
    
    void run_case() {
        int m, t;
        memset(bit, 0, sizeof(bit));
        cin >> n >> m;
        for(int i = 0; i < m; ++i) {
            cin >> t; bit[__builtin_ctz(t)]++;
        }
        cout << solve() << "
    ";
    }
    
    int main() {
        ios::sync_with_stdio(false), cin.tie(0);
        //cout.setf(ios_base::showpoint);cout.precision(10);
        int t; cin >> t;
        while(t--)
        run_case();
        cout.flush();
        return 0;
    }
    View Code
  • 相关阅读:
    linux I2C 读写 tlv320dac3100
    ubuntu lfs
    安装和使用花生壳(linux)
    vim 配置
    vim
    gnome2 恢复默认 panel
    ubuntu 挂在 jffs2 文件
    gstreamer 播放
    gstreamer 环境变亮设置
    探讨【IGE】的源代码【五】。
  • 原文地址:https://www.cnblogs.com/GRedComeT/p/12303814.html
Copyright © 2011-2022 走看看