CodeForces484A——Bits(贪心算法)
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.
Input
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).
Output
For each query print the answer in a separate line.
Sample test(s)
Input
3
1 2
2 4
1 10
Output
1
3
7
题目大意:
给定L,R,输出X,X为[L,R]中化为二进制之后一的个数最多的那个数,(同时存在多个解,输出最小的那个)。
解题思路:
设L,R的从高位开始有t1位相同,那么根据常识,x的前t1位(从高位开始数)必然也与LR相同。又因为L<=R,那么t1+1位不同,必然是L为0,R为1。
此时将x的t1+1位赋值为0,后面所有为都为1。那么求出的X: L<=x<R 成立,且在[L,R)区间中为最优解。
存在特殊情况:R换成二进制之后全部为1,所以要加一个判断。判断x在t1+1位为1的时候是否绝对大于R,绝对大于的话,就将t1+1赋值为0;反之,赋值为1。
Code:
/*************************************************************************
> File Name: CF484A.cpp
> Author: Enumz
> Mail: 369372123@qq.com
> Created Time: 2014年11月06日 星期四 01时49分25秒
************************************************************************/ #include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<list>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
#include<cmath>
#include<bitset>
#include<climits>
#define MAXN 100000
#define LL long long
using namespace std;
int main()
{
LL a,b,ans;
int T,k;
cin>>T;
while (T--)
{
ans=;
cin>>a>>b;
int i;
bool flag=;
for (i=; i>=; i--)
{
if (flag)
{
ans+=1LL<<i;
continue;
}
if((a&(1LL<<i))==(b&(1LL<<i)))
{
if ((a&(1LL<<i))!=)
ans+=1LL<<i;
}
else
{
flag=;
k=i;
i++;
}
}
if (ans>b)
ans=ans^(1LL<<k);
cout<<ans<<endl;
}
return ;
}
CodeForces484A——Bits(贪心算法)的更多相关文章
- CodeForces484A Bits(贪心)
CodeForces484A Bits(贪心) CodeForces484A 题目大意:给出范围[A.B].期望你给出某个数X满足X属于[A,B],而且X转成二进制的1的个数最多.假设有多个给出最小的 ...
- 贪心算法——Huffman 压缩编码的实现
1. 如何理解 "贪心算法" 假设我们有一个可以容纳 100 Kg 物品的背包,可以装各种物品.我们有以下 5 种豆子,每种豆子的总量和总价值都各不相同.怎样装才能让背包里豆子的总 ...
- 【ybtoj】贪心算法例题
[基础算法]第二章 贪心算法 例一 奶牛晒衣服 题目描述 有n件衣服,第i件衣服的湿度为h. 在自然条件下,每件衣服每分钟都可以自然晒干A点湿度. 在烘干机作用下,可以选择一件衣服,用一分钟的时间晒干 ...
- 贪心算法(Greedy Algorithm)
参考: 五大常用算法之三:贪心算法 算法系列:贪心算法 贪心算法详解 从零开始学贪心算法 一.基本概念: 所谓贪心算法是指,在对问题求解时,总是做出在当前看来是最好的选择.也就是说,不从整体最优上加以 ...
- 算法导论----贪心算法,删除k个数,使剩下的数字最小
先贴问题: 1个n位正整数a,删去其中的k位,得到一个新的正整数b,设计一个贪心算法,对给定的a和k得到最小的b: 一.我的想法:先看例子:a=5476579228:去掉4位,则位数n=10,k=4, ...
- LEETCODE —— Best Time to Buy and Sell Stock II [贪心算法]
Best Time to Buy and Sell Stock II Say you have an array for which the ith element is the price of a ...
- ACM_ICPC hdu-2111(简单贪心算法)
一道非常简单的贪心算法,但是要注意输入的价值是单位体积的价值,并不是这个物品的总价值!#include <iostream> #include <stdio.h> #inclu ...
- 基于贪心算法的几类区间覆盖问题 nyoj 12喷水装置(二) nyoj 14会场安排问题
1)区间完全覆盖问题 问题描述:给定一个长度为m的区间,再给出n条线段的起点和终点(注意这里是闭区间),求最少使用多少条线段可以将整个区间完全覆盖 样例: 区间长度8,可选的覆盖线段[2,6],[1, ...
- 增强学习贪心算法与Softmax算法
(一) 这个算法是基于一个概率来对探索和利用进行折中:每次尝试时,以概率进行探索,即以均匀概率随机选取一个摇臂,以的概率进行利用,即以这个概率选择当前平均奖赏最高的摇臂(如有多个,则随机选取). 其中 ...
随机推荐
- 团队作业php
<?php$kouwei=$_GET["select"];$daxiao=$_GET["RadioGroup1"];$peiliao=$_GET[&quo ...
- 关于Liferay所有的能够进行自定义和扩展的东西的总结
非常超级什么有用的东西,amazing. 虽然不是宝宝写的. Fantastic Extension Points - And Where to Find Them
- [Z] 深入浅出 Systemd
1. Systemd 的简介和特点 Systemd 是 Linux 系统中最新的初始化系统(init),它主要的设计目标是克服 sysvinit 固有的缺点,提高系统的启动速度.systemd 和 u ...
- Node.js 学习(一) 安装配置
Windowv 上安装Node.js Windows 安装包(.msi) : 32 位安装包下载地址 : http://nodejs.org/dist/v0.10.26/node-v0.10.26-x ...
- elasticsearch-查询的基本语法
elasticsearch的查询有两部分组成:query and filter. 两者的主要区别在于:filter是不计算相关性的,同时可以cache.因此,filter速度要快于query. 先记录 ...
- gvim编辑文件到github乱码
with below _vimrc settings, code uploaded to GitHub will display with proper encoding set encoding=u ...
- Shiro workshop
吃掉<Shiro教程>的精要部分,Go ahead!
- PE文件结构详解
(注:最左边是文件头的偏移量.) IMAGE_DOS_HEADER STRUCT { +0h WORD e_magic // Magic DOS signature MZ(4Dh 5Ah) DOS可执 ...
- SEO网站优化方案
学习许多前辈的经验,看到一些比较有价值的seo优化方案,特记录一下,对照自己的操作之路,新人也可借鉴一二,下面是从卢松松博客看到的文章.高手直接跳过,请勿喷水. 一个完整的SEO优化方案主要由四个小组 ...
- 使用tomcat7创建异步servlet
该篇文章翻译自:http://developerlife.com/tutorials/?p=1437 一.简介 Servlet API 3.0 之前,需要使用类似Comet的方式来实现创建异步的Ser ...