题目类型:位运算

传送门:>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. npm 安装及使用

    1. 安装 npm 安装完node后,就自动完成npm的安装. 2. 常用的npm命令 #安装包 npm install <pkg> 或者 npm install <pkg>@ ...

  2. 华为有AI,这场转型战有点大

    华为有AI,这场转型战有点大 https://mp.weixin.qq.com/s/qnUP5cgbNxXcAT82NQARtA 李根 发自 凹非寺 量子位 报道 | 公众号 QbitAI 华为有AI ...

  3. windows蓝屏代码

    原始链接 引用自  https://docs.microsoft.com/zh-cn/windows-hardware/drivers/debugger/bug-check-code-referenc ...

  4. Linux系统性能分析工具 sar--系统活动情况报告

    1.结论: sar 命令是linux系统上,分析系统性能的常用工具,可以查看cpu.内存.磁盘IO.文件读写.系统调用, 2.sar会有一个定时任务,定期记录当前系统信息到  /var/log/sa/ ...

  5. Easyui 修改|新增jquery-easyui icon图标

    修改|新增jquery-easyui icon图标 by:授客 QQ:1033553122 测试环境 jquery-easyui-1.5.3 修改配置文件 打开jquery-easyui-1.5.3\ ...

  6. 关于flutter插件地图的使用flutter_map

    关于flutter插件地图的使用flutter_map flutter_map A Dart implementation of Leaflet for Flutter apps.一个基于leafle ...

  7. Visual Studio无法调试

    一.最近Visual studio调试不起来,运行完报错 二.解决方法 打开  调试>>>>选项>>>>常规>>>对ASP.NET启用 ...

  8. adb.exe 安卓测试桥的使用

    一.android SDK提供了几个工具 (在SDK下build-tools目录下的工具) dx.bat ----------->把java编译器编译生成的.class 文件 ,变成一个文件,让 ...

  9. gradlew和gradle的区别

    概念理解 gradlew就是对gradle的包装和配置,gradlew是gradle Wrapper,Wrapper的意思就是包装. 因为不是每个人的电脑中都安装了gradle,也不一定安装的版本是要 ...

  10. vue 路由变化页面数据不刷新问题(缓存)

    每天记录一点点,把我遇到的问题记录下来, 希望可以帮助到更多和我遇到同样问题的人. 问题描述:通过调接口,动态显示帮助页面的问题列表, 问题列表有多级,当点击的这个问题没有下一级问题的时候跳入内容页. ...