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) — ...
随机推荐
- 文件查找工具Everything小工具的使用
Everything 小工具的使用: 首先它是一款基于名称实时定位文件和目录的搜索工具,有以下几个优点: 快速文件索引 快速文件搜索 较低资源占用 轻松分享文件索引 实时跟踪文件更新 通过使用ever ...
- JAVA中int与String类型的相互转换
Java的int和String类型间互相转换,小功能但是经常用到,下面是几种实现的方法: 字符串类型String转换成整数int 1. int i = Integer.parseInt([String ...
- Android开发之ListView添加多种布局效果演示
在这个案例中展示的新闻列表,使用到ListView控件,然后在适配器中添加多种布局效果,这里通过重写BaseAdapter类中的 getViewType()和getItemViewType()来做判断 ...
- Spring/Java error: namespace element 'annotation-config' … on JDK 1.5 and higher
Extract the jar file: mkdir spring cd spring jar xvf ../spring.jar Check the Spring version in META- ...
- 20145239 杜文超 实验四 Android开发基础
20145239实验四 Android开发基础 实验内容 基于Android Studio开发简单的Android应用并部署测试 了解Android组件.布局管理器的使用 掌握Android中事件处理 ...
- join()方法作用
当在主线程当中执行到t1.join()方法时,就认为主线程应该把执行权让给t1 废话不多说看代码: package com.toov5.thread; public class JoinThreadT ...
- C# 操作SQLServer SMO中遇到的几个问题
业务需求:需要读取数据库中的所有库,得到库之后可以再做后续操作.(win 7 vs2010 sqlserver2008r2) 在读取数据库名的时候,就需要用到Microsoft.SqlServer. ...
- BZOJ 1629 [Usaco2005 Nov]Cow Acrobats:贪心【局部证明】
题目链接:http://begin.lydsy.com/JudgeOnline/problem.php?id=1332 题意: 有n头牛在“叠罗汉”. 第i头牛的体重为w[i],力量为s[i]. 一头 ...
- firfox 和 chrome 移动端Web开发页面调试
Firefox浏览: 1."Alt+t" 选择工具栏中的“工具”>Web开发者工具>查看器>点击红框所指的地方 或者 F12 [当然这个歌前提是你没有安装fire ...
- linux命令学习笔记(46):vmstat命令
vmstat是Virtual Meomory Statistics(虚拟内存统计)的缩写,可对操作系统的虚拟内存.进程.CPU活动 进行监控.他是对系统的整体情况进行统计,不足之处是无法对某个进程进行 ...