Codeforces Round #276 (Div. 1) 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.
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).
For each query print the answer in a separate line.
3
1 2
2 4
1 10
1
3
7
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位补齐,贪心从小位到大位的补齐,一定是最优
/// #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=,f=;char ch=getchar();
while(ch<''||ch>''){
if(ch=='-')f=-;ch=getchar();
}
while(ch>=''&&ch<=''){
x=x*+ch-'';ch=getchar();
}return x*f;
}
//****************************************
const int N=+;
#define mod 10000007
#define inf 1000000001
#define maxn 10000 int d[];
ll test(ll x,ll y) {
for(int i=;i<;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=;i<=n;i++) {
cin>>l>>r;
cout<<test(l,r)<<endl;
}
return ;
}
代码
Codeforces Round #276 (Div. 1) A. Bits 贪心的更多相关文章
- Codeforces Round #276 (Div. 1) A. Bits 二进制 贪心
A. Bits Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/484/problem/A Des ...
- CF&&CC百套计划4 Codeforces Round #276 (Div. 1) A. Bits
http://codeforces.com/contest/484/problem/A 题意: 询问[a,b]中二进制位1最多且最小的数 贪心,假设开始每一位都是1 从高位i开始枚举, 如果当前数&g ...
- Codeforces Round #276 (Div. 2)C. Bits(构造法)
这道题直接去构造答案即可. 对于l的二进制表示,从右到左一位一位的使其变为1,当不能再变了(再变l就大于r了)时,答案就是l. 这种方法既可以保证答案大于等于l且小于等于r,也可以保证二进制表示时的1 ...
- Codeforces Round #276 (Div. 1)D.Kindergarten DP贪心
D. Kindergarten In a kindergarten, the children are being divided into groups. The teacher put t ...
- C. Bits (Codeforces Round #276 (Div. 2) )
题目大意:给你两个数l,r(l<r),求一个数是大于等于l且小于等于r的数中二进制数的1的个数最多,如果1的个数相同则取最小的那个(翻译渣,请见谅!) 思路:把左区间L化为二进制,再把左区间的二 ...
- codeforces 484a//Bits// Codeforces Round #276(Div. 1)
题意:给出区间[ll,rr],求中间一个数二进制表示时一的个数最多. 写出ll和rr的二进制,设出现第一个不同的位置为pos(从高位到低位),找的数为x,那么为了使x在[ll,rr]内,前pos-1个 ...
- Codeforces Round #382 (Div. 2)B. Urbanization 贪心
B. Urbanization 题目链接 http://codeforces.com/contest/735/problem/B 题面 Local authorities have heard a l ...
- Codeforces Round #180 (Div. 2) B. Sail 贪心
B. Sail 题目连接: http://www.codeforces.com/contest/298/problem/B Description The polar bears are going ...
- Codeforces Round #374 (Div. 2) B. Passwords 贪心
B. Passwords 题目连接: http://codeforces.com/contest/721/problem/B Description Vanya is managed to enter ...
随机推荐
- 使用WindowBuilder设计Swing程序
Swing程序表示Java的客户端窗体程序,除了通过手动编写代码的方式设计Swing程序之外,Eclipse中还提供了一种WindowBuilder工具,该工具是一种非常好用的Swing可视化开发工具 ...
- Android 基础知识图谱
四大组件 Activity Service BroadcastReceiver ContentProvider Application 常用组件 Fragment RecyclerView WebVi ...
- MySQL 多表批量更新
使用inner join 进行表更新sql 与mysql 的区别: SQL UPDATE W SET W.字段=新数据 FROM 表a W INNER JOIN 表B d ON W.wID=D.wid ...
- JS——行内式注册事件
html中行内调用function的时候,是通过window调用的function,所以打印this等于打印window,所以在使用行内注册事件时务必传入参数this <!DOCTYPE htm ...
- SQL基本操作——创建索引
CREATE INDEX 语句用于在表中创建索引.在不读取整个表的情况下,索引使数据库应用程序可以更快地查找数据. 索引:您可以在表中创建索引,以便更加快速高效地查询数据.用户无法看到索引,它们只能被 ...
- 怎么搭建Hibernate对象持久化框架?
DBC:(Java Data Base Connectivity)java数据库连接 java.sql包提供JDBC API,可通过它编写访问数据库的程序代码.其中常用的接口和类包括下面内容: Dri ...
- db2 jdbc连接字符串中 指定currentSchema
场景:连接DB2数据库的,jdbc的连接字符串中没有给当前的数据源用户指定默认的schema,而当前的数据源用户下可能有多个schema,则会使用数据源用户默认的schema. 例如:admin用户的 ...
- Oracle基础理论笔记(一):模式概念
---oracle 10g 1.在oracle数据库中,数据对象是以模式为单位进行组织和管理的.模式是指一系列的逻辑数据结构或对象的集合. 2.模式与用户名相对应,一个模式只能对应一个用户,并且该模式 ...
- Java 基础入门随笔(2) JavaSE版——关键字、进制转换、类型转换
1.Java语言-关键字 关键字:被java语言赋予了特殊含义的词,特点是所有的字母都为小写. java涉及到的关键字整理: 用于定义数据类型的关键字 class interface byte sho ...
- Python之进程 基础知识 上
阅读目录 理论知识 操作系统背景知识 什么是进程 进程调度 进程的并发与并行 同步\异步\阻塞\非阻塞 进程的创建与结束 在python程序中的进程操作 multiprocess模块 进程的创建和mu ...