• Codeforces Round #276 (Div. 1) A. Bits 贪心


    A. Bits
     

    Let's denote as  the number of bits set ('1' bits) in the binary representation of the non-negative integer x.

    You are given multiple queries consisting of pairs of integers l and r. For each query, find the x, such that l ≤ x ≤ r, and is maximum possible. If there are multiple such numbers find the smallest of them.

    Input

    The first line contains integer n — the number of queries (1 ≤ n ≤ 10000).

    Each of the following n lines contain two integers li, ri — the arguments for the corresponding query (0 ≤ li ≤ ri ≤ 1018).

    Output

    For each query print the answer in a separate line.

    Sample test(s)
    input
    3
    1 2
    2 4
    1 10
    output
    1
    3
    7
    Note

    The binary representations of numbers from 1 to 10 are listed below:

    110 = 12

    210 = 102

    310 = 112

    410 = 1002

    510 = 1012

    610 = 1102

    710 = 1112

    810 = 10002

    910 = 10012

    1010 = 10102

     题意:给n个询问,每次询问你l,r之间的数,在二进制下1位数最多的是哪个数

    题解:我们从l向上构造,在0位补齐,贪心从小位到大位的补齐,一定是最优

    ///1085422276
    
    #include<bits/stdc++.h>
    using namespace std;
    #pragma comment(linker, "/STACK:102400000,102400000")
    using namespace std ;
    typedef long long ll;
    typedef unsigned long long ull;
    #define mem(a) memset(a,0,sizeof(a))
    #define pb push_back
    inline ll read()
    {
        ll x=0,f=1;char ch=getchar();
        while(ch<'0'||ch>'9'){
            if(ch=='-')f=-1;ch=getchar();
        }
        while(ch>='0'&&ch<='9'){
            x=x*10+ch-'0';ch=getchar();
        }return x*f;
    }
    //****************************************
    const int  N=6000+50;
    #define mod 10000007
    #define inf 1000000001
    #define maxn 10000
    
    int d[5000];
    ll test(ll x,ll y) {
         for(int i=0;i<63;i++) {
            if(!(x&(1ll<<i))&&x+(1ll<<i)<=y)
              x+=(1ll<<i);
         }
        return x;
    }
    int main() {
        int n=read();ll l,r;
        for(int i=1;i<=n;i++) {
           cin>>l>>r;
            cout<<test(l,r)<<endl;
        }
        return 0;
    }
    代码
  • 相关阅读:
    python 中关于kafka的API
    python 中对json的操作
    python 错误--UnboundLocalError: local variable '**' referenced before assignment
    storm问题记录(1) python 不断向kafka中写消息,spout做为消费者从kafka中读消息并emit给bolt,但是部分消息没有得到bolt的处理
    nodejs+kafka+storm+hbase 开发
    python构造数据
    Head first java中提到的学习方法,很受用
    【机器学习 第2章 学习笔记】模型评估与选择
    路书
    二分搜索
  • 原文地址:https://www.cnblogs.com/zxhl/p/4965099.html
Copyright © 2020-2023  润新知