zoukankan      html  css  js  c++  java
  • CodeForces484A Bits(贪心)

    CodeForces484A Bits(贪心)

    CodeForces484A

    题目大意:给出范围【A。B】。期望你给出某个数X满足X属于【A,B】,而且X转成二进制的1的个数最多。假设有多个给出最小的数。

    解题思路:由于须要1最多,那么我们先将每一个位都放上1,假设这个数减掉了某一位的1还是超出了范围,那么就能够去掉这个1。假设去掉后发现比A小了,那么这个位置上的1是不能去掉的。直到找到一个数在范围内即为所求的。每次去掉的是高位的1那么在保证1的个数同样的情况下求得的自然是最小的数。

    代码:

    #include <cstdio>
    
    const int maxn = 63;
    typedef long long ll;
    
    int N;
    ll a, b;
    ll base[maxn];
    ll judge(ll tmp, int pos, int cnt);
    
    void init () {
        base[0] = 1L;
        for (int i = 1; i < maxn; i++) {
            base[i] = base[i - 1] * 2L;
        }
    }
    
    ll handle () {
    
        ll ans = base[maxn - 1] - 1;
        for (int i = maxn - 2; i >= 0; i--) {
            if (ans - base[i] > b)
                ans -= base[i];
            else if (ans - base[i] >= a && ans - base[i] <= b)
                return ans - base[i];
        }
    }
    
    int main () {
    
        scanf ("%d", &N);
        init();
        while (N--) {
            scanf ("%lld%lld", &a, &b);     
            printf ("%lld
    ", handle());
        }
        return 0;
    }
  • 相关阅读:
    246.Strobogrammatic Number
    245.Shortest Word Distance III
    244.Shortest Word Distance II
    243.Shortest Word Distance
    242. Valid Anagram
    241. Different Ways to Add Parentheses
    240.Search in a 2D Matrix II
    239. Sliding Window Maximum
    238. Product of Array Except Self
    postgres 行列转换
  • 原文地址:https://www.cnblogs.com/brucemengbm/p/7189550.html
Copyright © 2011-2022 走看看