Luogu P4109 [HEOI2015]定价 贪心

思路:找规律?$or$贪心。

提交:1次

题解:

发现:若可以构成$X0000$,答案绝对不会再在数字最后把$0$改成其他数;

若可以构成$XX50...0$更优。

所以左端点增加的步长是增加的($inc=pow(10,x),x$递增)。

所以去掉了一些不必要的枚举。

#include<cstdio>
#include<iostream>
#include<cmath>
#define R register int
using namespace std;
#define ull unsigned long long
#define ll long long
#define pause (for(R i=1;i<=10000000000;++i))
#define In freopen("NOIPAK++.in","r",stdin)
#define Out freopen("out.out","w",stdout)
namespace Fread {
static char B[1<<15],*S=B,*D=B;
#ifndef JACK
#define getchar() (S==D&&(D=(S=B)+fread(B,1,1<<15,stdin),S==D)?EOF:*S++)
#endif
inline int g() {
    R ret=0,fix=1; register char ch; while(!isdigit(ch=getchar())) fix=ch=='-'?-1:fix;
    if(ch==EOF) return EOF; do ret=ret*10+(ch^48); while(isdigit(ch=getchar())); return ret*fix;
} inline bool isempty(const char& ch) {return (ch<=36||ch>=127);}
inline void gs(char* s) {
    register char ch; while(isempty(ch=getchar()));
    do *s++=ch; while(!isempty(ch=getchar()));
}
} using Fread::g; using Fread::gs;
namespace Luitaryi {
int T,l,r; ll ans;
const ll Inf=1E+9;
inline void main() {
    T=g(); while(T--) { R mn=Inf;
        l=g(),r=g();
        while(l<=r) {
            R x=l,cnt=0;
            while(x%10==0) x/=10,++cnt;
            R y=x,len=0,lst=x%10;
            while(y) y/=10,++len;
            R tmp=2*len-(lst==5);
            if(mn>tmp) mn=tmp,ans=l;
            l+=pow(10,cnt);
        } printf("%lld\n",ans);
    }
}
} 
signed main() {
    Luitaryi::main(); return 0;
}

 

posted @ 2019-07-14 23:33  LuitaryiJack  阅读(119)  评论(0编辑  收藏  举报