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

BZOJ 4029 HEOI2015 定价 数位贪心

时间:2015-04-29 13:44:29      阅读:132      评论:0      收藏:0      [点我收藏+]

标签:bzoj   bzoj4029   数位dp   贪心   

题目大意:定义一个数的荒谬程度为去掉末尾所有0后的数字数量?2(若末尾为5则荒谬程度减掉1),求[l,r]区间内荒谬程度最小的数字(若多个相同取最小)
从高位往低位贪心即可。
注意500的荒谬程度比100低

#include <assert.h>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
int Calculate(long long l,long long r)
{
    long long temp,ans=0;
    for(temp=1;temp<=l;temp*=10);
    if(temp/2>=l&&temp/2<=r)
        return temp/2;
    if(temp*5<=r)
        return temp*5;
    temp/=10;
    for(;temp;temp/=10)
    {
        long long target;

        target=l-l%temp+(bool)(l%temp)*temp; 
        if( r>=target )
            return ans+target;

        if(l%temp<=temp/2)
        {
            target=l-l%temp+temp/2;
            if(r>=target)
                return ans+target;
        }
        ans+=l-l%temp;
        l%=temp;r%=temp;
    }
    assert(false);
}
int main()
{
    //freopen("4029.in","r",stdin);
    //freopen("4029.out","w",stdout);
    int T,l,r;
    for(cin>>T;T;T--)
    {
        scanf("%d%d",&l,&r);
        printf("%d\n",Calculate(l,r));
    }
    return 0;
}

BZOJ 4029 HEOI2015 定价 数位贪心

标签:bzoj   bzoj4029   数位dp   贪心   

原文地址:http://blog.csdn.net/popoqqq/article/details/45363863

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