您的位置:首页 > 其它

BZOJ 4029 HEOI2015 定价 数位贪心

2015-04-29 11:07 253 查看
题目大意:定义一个数的荒谬程度为去掉末尾所有00后的数字数量∗2*2(若末尾为55则荒谬程度减掉11),求[l,r][l,r]区间内荒谬程度最小的数字(若多个相同取最小)

从高位往低位贪心即可。

注意500的荒谬程度比100低

[code]#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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: