洛谷——P4109 [HEOI2015]定价

P4109 [HEOI2015]定价

 

模拟(有点儿贪心)

 

题目要求在区间$l,r$中$x$后导0尽量多,且除去后导0之外,最后一个数尽量是$5$才最优

从$l$到$r$依次考虑,

假设当前考虑到$5000$,$r=60000$,

那么在$5000$到$6000$之间的都不用考虑,因为比当前优的只能是后导0比其多的(emmm),应该是后导0与其一致或比其多并且符合上述条件的

 

那么每次$l$都要加上$l$这个数的后导0的个数个10

#include<bits/stdc++.h>

using namespace std;

int T,l,r;

int pow(int a,int b){
    int res=1;
    for(;b;b>>=1,a=a*a)
        if(b&1) res=res*a;
    return res;
}

int main()
{
    scanf("%d",&T);
    while(T--){
        int maxn=0x7fffffff;
        scanf("%d%d",&l,&r);
        //->10^N
        int ans;
        while(l<=r){
            int x=l,sum=0,len=0,y;
            while(x) {
                if(!(x%10)) sum++,x/=10;
                else break;
            }
            y=x;
            while(x) x/=10,len++;
            len*=2;
            if(y%10==5) len--;
            if(len<maxn) maxn=len,ans=l;
            l+=pow(10,sum);
        }
        printf("%d\n",ans);
    }
    
    return 0;
}

 

posted @ 2018-10-22 17:46  清风我已逝  阅读(161)  评论(0编辑  收藏  举报