题意

输出[l, r]中二进制数字最多的数字。

思路

贪心,从r的最高位1开始和l比较,直到pos[r] = 1, pos[l] = 0,这时候就可以把r的当前位置置零,之前的位置全部变成1,这个值显然>= l。

然后还要判断一下,因为可能变了之后的数量还没有变之前多。

代码

 
  1. #include <cstdio>
  2. #include <stack>
  3. #include <set>
  4. #include <iostream>
  5. #include <string>
  6. #include <vector>
  7. #include <queue>
  8. #include <functional>
  9. #include <cstring>
  10. #include <algorithm>
  11. #include <cctype>
  12. #include <string>
  13. #include <map>
  14. #include <cmath>
  15. #define LL long long
  16. #define SZ(x) (int)x.size()
  17. #define Lowbit(x) ((x) & (-x))
  18. #define MP(a, b) make_pair(a, b)
  19. #define MS(arr, num) memset(arr, num, sizeof(arr))
  20. #define PB push_back
  21. #define F first
  22. #define S second
  23. #define ROP freopen("input.txt", "r", stdin);
  24. #define MID(a, b) (a + ((b - a) >> 1))
  25. #define LC rt << 1, l, mid
  26. #define RC rt << 1|1, mid + 1, r
  27. #define LRT rt << 1
  28. #define RRT rt << 1|1
  29. #define BitCount(x) __builtin_popcount(x)
  30. #define BitCountll(x) __builtin_popcountll(x)
  31. #define LeftPos(x) 32 - __builtin_clz(x) - 1
  32. #define LeftPosll(x) 64 - __builtin_clzll(x) - 1
  33. const double PI = acos(-1.0);
  34. const int INF = 0x3f3f3f3f;
  35. using namespace std;
  36. const int MAXN = 1000000 + 10;
  37. const int MOD = 1e9 + 7;
  38. typedef pair<int, int> pii;
  39. typedef vector<int>::iterator viti;
  40. typedef vector<pii>::iterator vitii;
  41. int main()
  42. {
  43. //ROP;
  44. int T, i, j;
  45. scanf("%d", &T);
  46. while (T--)
  47. {
  48. LL l, r;
  49. cin >> l >> r;
  50. int pos = LeftPosll(r);
  51. LL ans = 0;
  52. for (; pos >= 0; pos--)
  53. {
  54. LL tmp = (r & (1ll << pos));
  55. if (tmp && (l & (1ll << pos)) == 0)
  56. {
  57. pos--;
  58. while (pos >= 0)
  59. {
  60. ans |= (1ll << pos);
  61. pos--;
  62. }
  63. }
  64. else if (tmp) ans |= (1ll << pos);
  65. }
  66. if (BitCountll(ans) < BitCountll(r)) ans = r;
  67. cout << ans << endl;
  68. }
  69. return 0;
  70. }

Codeforces 484A - Bits (贪心)相关推荐

  1. CodeForces - 1208F Bits And Pieces(SOSdp+贪心)

    题目链接:点击查看 题目大意:给出一个长度为 nnn 的序列,求出贡献最大的三元对:(i,j,k)(i,j,k)(i,j,k),满足 i<j<ki<j<ki<j<k ...

  2. Codeforces1600数学day5[找规律CodeForces - 1059C,贪心数学A - Banh-mi CodeForces - 1062C ]

    A - Banh-mi CodeForces - 1062C 题目大意:就是开始给你一个01序列,和q次询问,每次询问会给你一个区间,每次你会从这个区间内拿出一个数,在区间内的其他未被拿走的数字会加上 ...

  3. CodeForces - 1494D Dogeforces(贪心+构造)

    题目链接:点击查看 题目大意:给出 nnn 个叶子结点和一个 n∗nn*nn∗n 的 LCALCALCA 矩阵,其中 LCALCALCA 表示的是最近公共祖先节点的权值,现在需要构造出一棵自顶向下权值 ...

  4. Solve The Maze CodeForces - 1365D(贪心+dfs)

    Vivek has encountered a problem. He has a maze that can be represented as an n×m grid. Each of the g ...

  5. K-periodic Garland CodeForces - 1353E(贪心)

    You are given a garland consisting of n lamps. States of the lamps are represented by the string s o ...

  6. Dreamoon Likes Coloring CodeForces - 1330C(贪心+思维)

    Dreamoon likes coloring cells very much. There is a row of n cells. Initially, all cells are empty ( ...

  7. Codeforces 1041C(贪心+set)

    传送门 题面: C. Coffee Break time limit per test 2 seconds memory limit per test 256 megabytes input stan ...

  8. CodeForces 798D 思维,贪心

    CodeForces 798D 题意:长度为 n的两个数组 a[]和 b[],要找出 k ( k<=n/2+1 )个下标,使得在两个数组中这 k个数的和乘上 2 要大于所有数的和. tags: ...

  9. Codeforces 1203E Boxers(贪心)

    题目链接:https://codeforces.com/problemset/problem/1203/E 题意:给定n个数字,每个数字可以进行一次+1或是-1的变换(也可以不变),问通过对部分数字适 ...

最新文章

  1. 数据结构实验——中缀表达式转为后缀表达式
  2. 增强QQ空间的统计功能
  3. mysql合并表中的数据
  4. Linux程序设计实验项目六,《linux程序设计》实验教学大纲
  5. 数据结构--直接插入排序
  6. Delphi:校验手机号及身份证号
  7. PyTorch系列入门到精通——图像预处理transforms
  8. Python自动化测试的学习笔记
  9. shell 并行执行与串行执行
  10. SSH 登录太慢的解决方法
  11. SI 9000 及阻抗匹配学习笔记(一)
  12. 手持式频谱分析仪TFN RMT720A 频谱分析 基站分析 路测覆盖
  13. c语言中lu是什么数据类型,C语言编程入门之--第四章C语言基本数据类型
  14. LPC2294看门狗定时器
  15. Windows小工具 tcping
  16. python的datetime.date日期计算——计算当月第一天、当月最后一天、前月第一天、前月最后一天、次月第一天、次月最后一天、去年的当天日期等
  17. 2022起重机械指挥考试题模拟考试题库及在线模拟考试
  18. 自学编程难吗?如何学?
  19. 记录几个免费质量高的图库视频库
  20. 贝叶斯公式的对数似然函数_最大似然法与似然函数

热门文章

  1. sql数据库习题总集
  2. 首席新媒体运营黎想教程:电商运营必须掌握的5个公式
  3. Windows如何彻底删除.sys后缀的流氓文件
  4. 绊脚石乃是进身之阶 jzoj 2017.8.15 B组
  5. outlook 签名_如何在Outlook 2013中创建新签名
  6. 【论文阅读】SyncPerf: Categorizing, Detecting, and Diagnosing Synchronization Performance Bugs
  7. 室内定位:基于NB/LTE Cat.1蜂窝网络的穿戴设备定位 BLE-4
  8. OpenGL视角LooAt及Perspective理解
  9. uva 10158 War
  10. 地面气象观测数据-A文件转excel方法