码迷,mamicode.com
首页 > 其他好文 > 详细

Codeforces Round #276 (Div. 1)

时间:2014-11-19 13:56:37      阅读:175      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   io   ar   color   os   sp   for   

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

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

bubuko.com,布布扣
#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)

标签:style   blog   http   io   ar   color   os   sp   for   

原文地址:http://www.cnblogs.com/icodefive/p/4107989.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!