CodeForces 484A Bits(水题)
1 second
256 megabytes
standard input
standard output
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
题意:给定l和r,找一个数x,满足 l<=x<=r,且x的二进制形式中1的数量要最多
思路:模拟一下,从高位到地位扫,如果r[p]==l[p],那么答案的p位就和他们一样,否则必有r[p]=1 && l[p]=0,那就让答案p位为0,后面其余位为1
擦。。过了pretest没过final test,原因是我数组忘记初始化了,好粗心。。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int INF = 1e9;
const double eps = 1e-;
const int N = ;
int cas = ; bool a[N],b[N],c[N];
int mx; void ltob(ll x,bool *arr)
{
int sz=;
while(x)
arr[sz++]=x&,x>>=;
if(mx<sz) mx=sz;
} bool judge(int p)
{
for(;p>=;p--)
if(a[p]==) return ;
return ;
} void work(int p)
{
if(p<) return;
if(judge(p))
for(int i=p;i>=;i--) c[i]=;
else{
if(a[p] == b[p]) c[p]=a[p],work(p-);
else{
for(int i=p-;i>=;i--)
c[i]=;
}
}
} void run()
{
ll l,r;
scanf("%I64d%I64d",&l,&r);
if(l==r) cout<<l<<endl;
else{
mx=;
memset(a,,sizeof(a));
memset(b,,sizeof(b));
ltob(r,a);
ltob(l,b); // cout<<"a:";for(int i=mx-1;i>=0;i--) printf("%d",a[i]); puts("");
// cout<<"b:";for(int i=mx-1;i>=0;i--) printf("%d",b[i]); puts("");
memset(c,,sizeof(c));
work(mx-);
// cout<<"c:";for(int i=mx-1;i>=0;i--) printf("%d",c[i]); puts("");
ll ans = , base = ;
for(int i=;i<mx;i++){
if(c[i]) ans+=base;
base<<=;
}
cout<<ans<<endl;
}
} int main()
{
#ifdef LOCAL
// freopen("case.txt","r",stdin);
#endif
int _;
scanf("%d",&_);
while(_--)
run();
return ;
}
CodeForces 484A Bits(水题)的更多相关文章
- Codeforces数据结构(水题)小结
最近在使用codeblock,所以就先刷一些水题上上手 使用codeblock遇到的问题 1.无法进行编译-------从setting中的编译器设置中配置编译器 2.建立cpp后无法调试------ ...
- CodeForces 705A Hulk (水题)
题意:输入一个 n,让你输出一行字符串. 析:很水题,只要判定奇偶性,输出就好. 代码如下: #pragma comment(linker, "/STACK:1024000000,10240 ...
- CodeForces 705B (训练水题)
题目链接:http://codeforces.com/problemset/problem/705/B 题意略解: 两个人玩游戏,解数字,一个数字可以被分成两个不同或相同的数字 (3可以解成 1 2) ...
- CodeForces 474B Worms (水题,二分)
题意:给定 n 堆数,然后有 m 个话询问,问你在哪一堆里. 析:这个题是一个二分题,但是有一个函数,可以代替写二分,lower_bound. 代码如下: #include<bits/stdc+ ...
- codeforces hungry sequence 水题
题目链接:http://codeforces.com/problemset/problem/327/B 这道题目虽然超级简单,但是当初我还真的没有想出来做法,囧,看完别人的代码恍然大悟. #inclu ...
- codeforces 377A. Puzzles 水题
A. Puzzles Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset/problem/33 ...
- Codeforces 484A - Bits 二进制找1
这题可以根据l, r 在二进制下的长度进行分类. l 的长度小于 r 的时候,有两种可能,一种是r 在二进制下是 1* 这种样子,故答案取 r : 一种是取答案为 (1LL << (r ...
- CodeForces 474A Keyboard (水题)
题意:给定一个键盘,然后一行字母,和一个字符,代表把那一行字母在键盘上左移还是右移一位. 析:没什么好说的,直接暴力就好. 代码如下: #include<bits/stdc++.h> us ...
- CodeForces - 682B 题意水题
CodeForces - 682B Input The first line of the input contains a single integer n (1 ≤ n ≤ 100 000) — ...
随机推荐
- oracle 存储过程(1)
说明 创建一个存储过程与编写一个普通的PL/SQL程序快有很多相似地方,比如:包括生命部分,执行部分和异常部分.但是两者之间实现细节还是有很多差别的,比如:创建存储过程需要使用procedure关键字 ...
- 关于Gradle和Gradle插件的问题
一. Gradle更新插件问题 当更新Andorid studio 的时候,你可能会接收到一条让你更新Gradle插件到最新版本的建议.在项目编译需要的基础上,你可以选择接受或者手动选择一个具体的 ...
- 九度OJ 1173:查找 (排序、查找)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:5808 解决:3163 题目描述: 输入数组长度 n 输入数组 a[1...n] 输入查找个数m 输入查找数字b[1...m ...
- 九度OJ 1065:输出梯形 (基础题)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:5379 解决:2939 题目描述: 输入一个高度h,输出一个高为h,上底边为h的梯形. 输入: 一个整数h(1<=h<=1000 ...
- RocksDB
RocksDB RocksDB is a high performance[1][2][3][4][5] embedded database for key-value data. It is a f ...
- EF学习和使用(三)Code First
Code First模式我们称之为"代码优先"模式.从某种角度来看.其实"Code First"和"Model First"区别并非太明显. ...
- JavaScript判断图片是否加载完成的三种方式 (转)
一.load事件 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 <!DOCTYPE HTML> <html> <head> ...
- POJ - 2251 Dungeon Master 【BFS】
题目链接 http://poj.org/problem?id=2251 题意 给出一个三维地图 给出一个起点 和 一个终点 '#' 表示 墙 走不通 '.' 表示 路 可以走通 求 从起点到终点的 最 ...
- 5.2 《锋利的jQuery》jQuery对表格的操作(选项卡/换肤)
表格隔行变色以及单选/复选 表格展开关闭 表格筛选 字体变大/缩小 选项卡 网页换肤 tip1: $("tr:odd")和$("tr:even")选择器索引是从 ...
- 细说后端模板渲染、客户端渲染、node 中间层、服务器端渲染(ssr)
细说后端模板渲染.客户端渲染.node 中间层.服务器端渲染(ssr) 前端与后端渲染方式的发展大致经历了这样几个阶段:后端模板渲染.客户端渲染.node 中间层.服务器端渲染(ssr). 1. 后端 ...