Codeforces484a--bits (greedy algorithm)

Source: Internet
Author: User

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 this l≤x≤r, and is maximum possible. If There is 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 and 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
Main topic:

Given l,r, the output x,x is the number that is the most numerous after the binary in [L,r], (there are multiple solutions at the same time, the output is the smallest one).

Problem Solving Ideas:

The l,r has the same T1 bit from the high position, so according to Common sense, the first T1 bit of x (starting from high) must also be the same as LR. And because L<=r, then t1+1 bit different, must be L for 0,r for 1.

At this point, the t1+1 bit of x is assigned a value of 0, followed by all 1. Then the X:L<=X<R is established and the optimal solution is obtained in the [L,r] interval.

There is a special case: R is converted to binary after all is 1, so add a judgment. Determine if x is absolutely greater than r when the t1+1 bit is 1, and is definitely greater than the t1+1 assignment to 0, whereas the value is 1.

Code:

1 /*************************************************************************2 > File Name:CF484A.cpp3 > Author:enumz4 > Mail: [email protected]5 > Created time:2014 November 06 Thursday 01:49 25 seconds6  ************************************************************************/7 8#include <iostream>9#include <cstdio>Ten#include <cstdlib> One#include <string> A#include <cstring> -#include <list> -#include <queue> the#include <stack> -#include <map> -#include <Set> -#include <algorithm> +#include <cmath> -#include <bitset> +#include <climits> A #defineMAXN 100000 at #defineLL Long Long - using namespacestd; - intMain () - { - LL A,b,ans; -     intt,k; inCin>>T; -      while(t--) to     { +ans=0; -Cin>>a>>b; the         inti; *         BOOLflag=0; $          for(i= -; i>=0; i--)Panax Notoginseng         { -             if(flag) the             { +ans+=1ll<<i; A                 Continue; the             } +             if((a& (1ll<<i)) = = (b& (1ll<<i))) -             { $                 if((a& (1ll<<i))! =0) $ans+=1ll<<i; -             } -             Else the             { -flag=1;Wuyik=i; thei++; -             } Wu         } -         if(ans>b) Aboutans=ans^ (1ll<<k); $cout<<ans<<Endl; -     } -     return 0; -}

Codeforces484a--bits (greedy algorithm)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.