题目类型:位运算

传送门:>Here<

题意:求区间\([L,R]\)内二进制中1的个数最多的那个数,如果有多解输出最小解

解题思路

想了15min就一遍A了

我们可以贪心地在\(L\)的基础上+1,从小的往大的加。根据二进制的性质,我们不可能把原来的1变成0,除非在更高位搞出一个新的1来。因为如果不在更高位搞出一个1,就肯定比\(L\)小了。而我们又不可能搞掉一个地位,去弄一个高位,因为这样不仅没有让1的个数增多,反而让数字更大了

因此我们的最优策略,就是从最低位开始给0的位补1。知道超过\(R\)为止

反思

贪心思想

Code

注意,\(C++\)中的平移运算符\(1<<x\)是不可以超过31位的(我就是因为这个调试了很久)。所以在\(long \ long\)范围下要么手打,要么\(1LL<<x\)

/*By DennyQi 2018*/
#include <cstdio>
#include <queue>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;
const int MAXN = 10010;
const int MAXM = 20010;
const int INF = 1061109567;
inline int Max(const int a, const int b){ return (a > b) ? a : b; }
inline int Min(const int a, const int b){ return (a < b) ? a : b; }
inline ll read(){
ll x = 0; int w = 1; register char c = getchar();
for(; c ^ '-' && (c < '0' || c > '9'); c = getchar());
if(c == '-') w = -1, c = getchar();
for(; c >= '0' && c <= '9'; c = getchar()) x = (x<<3) + (x<<1) + c - '0'; return x * w;
}
ll N,l,r,ans,num;
ll power2(ll x){
ll res = 1;
for(ll i = 1; i <= x; ++i) res *= 2;
return res;
}
signed main(){
N = read();
for(ll i = 1; i <= N; ++i){
l = read(), r = read();
num = 0;
for(ll j = 0; j < 61; ++j){
if(l & power2(j)){
++num;
}
}
ans = l;
for(ll j = 0; j < 61; ++j){
if((!(l & power2(j))) && (ans + power2(j) <= r)){
++num;
ans += power2(j);
}
}
printf("%I64d\n", ans);
}
return 0;
}

Codeforces484 A. Bits的更多相关文章

  1. [LeetCode] Number of 1 Bits 位1的个数

    Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also know ...

  2. [LeetCode] Reverse Bits 翻转位

    Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented in ...

  3. 【leetcode】Number of 1 Bits

    题目描述: Write a function that takes an unsigned integer and returns the number of '1' bits it has (als ...

  4. Leetcode-190 Reverse Bits

    #190. Reverse Bits Reverse bits of a given 32 bits unsigned integer. For example, given input 432615 ...

  5. CodeForces 485C Bits[贪心 二进制]

    C. Bits time limit per test1 second memory limit per test256 megabytes inputstandard input outputsta ...

  6. uva12545 Bits Equalizer

    uva12545 Bits Equalizer You are given two non-empty strings S and T of equal lengths. S contains the ...

  7. LeetCode Counting Bits

    原题链接在这里:https://leetcode.com/problems/counting-bits/ 题目: Given a non negative integer number num. Fo ...

  8. Number of 1 Bits(Difficulty: Easy)

    题目: Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also ...

  9. 解剖SQLSERVER 第五篇 OrcaMDF里读取Bits类型数据(译)

    解剖SQLSERVER 第五篇  OrcaMDF里读取Bits类型数据(译) http://improve.dk/reading-bits-in-orcamdf/ Bits类型的存储跟SQLSERVE ...

随机推荐

  1. 【Vue 2.x】指令的学习

      v-on作用于事件,简写@ v-bind作用于html元素的属性,简写: v-for作用于模板内的变量,和C#的foreach类似的用法 v-if和v-show条件渲染html元素 v-model ...

  2. 十分钟(小时)学习pandas

    十分钟学习pandas 一.导语 这篇文章从pandas官网翻译:链接,而且也有很多网友翻译过,而我为什么没去看他们的,而是去官网自己艰难翻译呢? 毕竟这是一个学习的过程,别人写的不如自己写的记忆深刻 ...

  3. 轨迹系列1——一种基于路网图层的GPS轨迹优化方案

    文章版权由作者李晓晖和博客园共有,若转载请于明显处标明出处:http://www.cnblogs.com/naaoveGIS/ 1.背景 GPS数据正常情况下有20M左右的偏移,在遇到高楼和桥梁等情况 ...

  4. RecycleView设置顶部分割线(记录一个坑)

    大家都知道,想给RecycleView设置分割线可以重写RecyclerView.ItemDecoration 项目过程中,遇到一个需求:RecycleView顶部有一条灰色的间隔,我想到了给Recy ...

  5. 学习安卓开发[3] - 使用RecyclerView显示列表

    在上一篇学习安卓开发[2] - 在Activity中托管Fragment中了解了使用Fragment的好处和方法,本次记录的是在进行列表展示时RecyclerView的使用. RecyclerView ...

  6. 牛客网:Java重命名文件

    项目介绍 不管是C/C++还是JAVA,都可能生成一些持久性数据,我们可以将数据存储在文件或数据库中,此项目主要训练学习Java对本地磁盘的文件重命名,例如C:\nowcoder.txt重命名C:\n ...

  7. C++ 死循环在语言层面的检测

    英文概念 Infinite loop without side-effects 这个目前只有CLang实现了这个C++特色 #include <iostream> int 费马定理() { ...

  8. SQL Server 2008初次启动

    一.关于安装 SQL Server 数据库的安装,经过自己的安装,总体还是比较容易,没有太多难度,安装包在网上也有很多,在此,就跳过安装的这一步. 二.初次启动SQL Server 安装完成数据库后, ...

  9. LVS负载均衡基础介绍及NET、DR模式配置

    LVS:术语: CIP:Client IP:客户端IP: VIP:Virtual Server IP:虚拟主机对外IP: RIP:Real Server IP:真实主机IP: DIP:Director ...

  10. SQLServer之创建全文索引

    创建全文索引的必须条件 必须具有全文目录,然后才能创建全文索引. 目录是包含一个或多个全文索引的虚拟容器. 使用SSMS数据库管理工具创建全文索引 1.连接数据库,选择数据库,选择数据表->右键 ...