首页 > 代码库 > Codeforces Round #276 (Div. 1)

Codeforces Round #276 (Div. 1)

给俩数, 求他俩之间二进制数中1最多的,有多个输出最小的;

贪心,从小到大加能加就加,最后可能碰到一个不能加了但是当前数比l小,那么就加上这个数,然后从大到小,能减就减,见到符合条件

#include<iostream>#include<stdio.h>#include<string.h>using namespace std;int main(){    long long n, l, r;    long long a[100];    long long s = 1, maxa = 1e18;    long long u;    for(long long i = 0;; i++){        a[i] = s;        s*=2;        u = i;        if(s > maxa)break;    }    //for(int i = 0; i <= u; i++)printf("%lld ", a[i]);    cin>>n;    while(n--){        cin>>l>>r;//printf("*%d", u);        long long sum = 0;        for(int i = 0; i <= u; i++){            if(sum + a[i] <= r){//cout<<sum<<" "<<a[i]<<endl;                sum += a[i];            }else if(sum < l){               sum += a[i];                for(int k = i-1; k >= 0; k--){                    if(sum - a[k] >= l){                        sum -= a[k];                    }                    if(l <= sum  && sum <= r)                        break;                }break;            }        }        cout<<sum<<endl;    }}
View Code

 

Codeforces Round #276 (Div. 1)