Codeforces 484A - Bits (贪心)
题意
输出[l, r]中二进制数字最多的数字。
思路
贪心,从r的最高位1开始和l比较,直到pos[r] = 1, pos[l] = 0,这时候就可以把r的当前位置置零,之前的位置全部变成1,这个值显然>= l。
然后还要判断一下,因为可能变了之后的数量还没有变之前多。
代码
#include <cstdio>
#include <stack>
#include <set>
#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <functional>
#include <cstring>
#include <algorithm>
#include <cctype>
#include <string>
#include <map>
#include <cmath>
#define LL long long
#define SZ(x) (int)x.size()
#define Lowbit(x) ((x) & (-x))
#define MP(a, b) make_pair(a, b)
#define MS(arr, num) memset(arr, num, sizeof(arr))
#define PB push_back
#define F first
#define S second
#define ROP freopen("input.txt", "r", stdin);
#define MID(a, b) (a + ((b - a) >> 1))
#define LC rt << 1, l, mid
#define RC rt << 1|1, mid + 1, r
#define LRT rt << 1
#define RRT rt << 1|1
#define BitCount(x) __builtin_popcount(x)
#define BitCountll(x) __builtin_popcountll(x)
#define LeftPos(x) 32 - __builtin_clz(x) - 1
#define LeftPosll(x) 64 - __builtin_clzll(x) - 1
const double PI = acos(-1.0);
const int INF = 0x3f3f3f3f;
using namespace std;
const int MAXN = 1000000 + 10;
const int MOD = 1e9 + 7;
typedef pair<int, int> pii;
typedef vector<int>::iterator viti;
typedef vector<pii>::iterator vitii;
int main()
{
//ROP;
int T, i, j;
scanf("%d", &T);
while (T--)
{
LL l, r;
cin >> l >> r;
int pos = LeftPosll(r);
LL ans = 0;
for (; pos >= 0; pos--)
{
LL tmp = (r & (1ll << pos));
if (tmp && (l & (1ll << pos)) == 0)
{
pos--;
while (pos >= 0)
{
ans |= (1ll << pos);
pos--;
}
}
else if (tmp) ans |= (1ll << pos);
}
if (BitCountll(ans) < BitCountll(r)) ans = r;
cout << ans << endl;
}
return 0;
}
Codeforces 484A - Bits (贪心)相关推荐
- CodeForces - 1208F Bits And Pieces(SOSdp+贪心)
题目链接:点击查看 题目大意:给出一个长度为 nnn 的序列,求出贡献最大的三元对:(i,j,k)(i,j,k)(i,j,k),满足 i<j<ki<j<ki<j<k ...
- Codeforces1600数学day5[找规律CodeForces - 1059C,贪心数学A - Banh-mi CodeForces - 1062C ]
A - Banh-mi CodeForces - 1062C 题目大意:就是开始给你一个01序列,和q次询问,每次询问会给你一个区间,每次你会从这个区间内拿出一个数,在区间内的其他未被拿走的数字会加上 ...
- CodeForces - 1494D Dogeforces(贪心+构造)
题目链接:点击查看 题目大意:给出 nnn 个叶子结点和一个 n∗nn*nn∗n 的 LCALCALCA 矩阵,其中 LCALCALCA 表示的是最近公共祖先节点的权值,现在需要构造出一棵自顶向下权值 ...
- 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 ...
- 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 ...
- Dreamoon Likes Coloring CodeForces - 1330C(贪心+思维)
Dreamoon likes coloring cells very much. There is a row of n cells. Initially, all cells are empty ( ...
- Codeforces 1041C(贪心+set)
传送门 题面: C. Coffee Break time limit per test 2 seconds memory limit per test 256 megabytes input stan ...
- CodeForces 798D 思维,贪心
CodeForces 798D 题意:长度为 n的两个数组 a[]和 b[],要找出 k ( k<=n/2+1 )个下标,使得在两个数组中这 k个数的和乘上 2 要大于所有数的和. tags: ...
- Codeforces 1203E Boxers(贪心)
题目链接:https://codeforces.com/problemset/problem/1203/E 题意:给定n个数字,每个数字可以进行一次+1或是-1的变换(也可以不变),问通过对部分数字适 ...
最新文章
- 数据结构实验——中缀表达式转为后缀表达式
- 增强QQ空间的统计功能
- mysql合并表中的数据
- Linux程序设计实验项目六,《linux程序设计》实验教学大纲
- 数据结构--直接插入排序
- Delphi:校验手机号及身份证号
- PyTorch系列入门到精通——图像预处理transforms
- Python自动化测试的学习笔记
- shell 并行执行与串行执行
- SSH 登录太慢的解决方法
- SI 9000 及阻抗匹配学习笔记(一)
- 手持式频谱分析仪TFN RMT720A 频谱分析 基站分析 路测覆盖
- c语言中lu是什么数据类型,C语言编程入门之--第四章C语言基本数据类型
- LPC2294看门狗定时器
- Windows小工具 tcping
- python的datetime.date日期计算——计算当月第一天、当月最后一天、前月第一天、前月最后一天、次月第一天、次月最后一天、去年的当天日期等
- 2022起重机械指挥考试题模拟考试题库及在线模拟考试
- 自学编程难吗?如何学?
- 记录几个免费质量高的图库视频库
- 贝叶斯公式的对数似然函数_最大似然法与似然函数
热门文章
- sql数据库习题总集
- 首席新媒体运营黎想教程:电商运营必须掌握的5个公式
- Windows如何彻底删除.sys后缀的流氓文件
- 绊脚石乃是进身之阶 jzoj 2017.8.15 B组
- outlook 签名_如何在Outlook 2013中创建新签名
- 【论文阅读】SyncPerf: Categorizing, Detecting, and Diagnosing Synchronization Performance Bugs
- 室内定位:基于NB/LTE Cat.1蜂窝网络的穿戴设备定位 BLE-4
- OpenGL视角LooAt及Perspective理解
- uva 10158	War
- 地面气象观测数据-A文件转excel方法