1970. [HEOI2015]定价
-
【问题描述】
在市场上有很多商品的定价类似于 999 元、4999 元、8999 元这样。它们和 1000 元、5000 元和 9000 元并没有什么本质区别,但是在心理学上会让人感觉便宜很多,因此也是商家常用的价格策略。不过在你看来,这种价格十分荒谬。于是你如此计算一个价格 p(p 为正整数)的荒谬程度:
1、首先将 p 看做一个由数字组成的字符串(不带前导 0);
2、然后,如果 p 的最后一个字符是 0,就去掉它。重复这一过程,直到 p 的最后一个字符不是 0;
3、记 p 的长度为 a,如果此时 p 的最后一位是 5,则荒谬程度为 2 * a - 1;否则为 2 * a。
例如,850 的荒谬程度为 3,而 880 则为 4,9999 的荒谬程度为 8。
现在,你要出售一样闲置物品,你能接受的定价在 [L, R] 范围内,你想要给出一个荒谬度最低的价格。
是我河北的省选题,从此我也是做过省选题的男人了!
但是用的是暴力法,还是有些羞愧。
暴力法枚举[L,R]的每个数,取荒谬度最低的价格。
然而会TLE。所以针对题目对暴力法进行优化:显然在末尾有k个0的情况下,枚举下一个数和枚举下一个末尾数字0的个数相同的数得到的结果相同。计算出当前枚举到的数字末尾0的个数,然后瞎搞一下就A了
#include<cstdio>
#include<iostream>
#include<algorithm>
#define COGS
using namespace std;
inline int read()
{
int x,f=1;
char ch;
while(ch=getchar(),!isdigit(ch)){
if(ch=='-')f=-1;
if(ch==EOF)return -1;
}
x=ch-48;
while(ch=getchar(),isdigit(ch))x=x*10+ch-48;
return x*f;
}
inline int getlen(int x){//数字长度
int sum=0;
while(x)x/=10,sum++;
return sum;
}
inline int getny(int x){//求数字末尾有几个0
int res=1;
while(x%10==0)res*=10,x/=10;
return res;
}
inline int getnum(int x){//求"荒谬度"
int tot=0;
while(x%10==0)x/=10;
tot=getlen(x);
if(x%10==5)return (tot<<1)-1;
return (tot<<1);
}
int main()
{
#ifdef COGS
string FileName="absurd";
freopen((FileName+".in").c_str(),"r",stdin);
freopen((FileName+".out").c_str(),"w",stdout);
#endif
int T;
T=read();
while(T--){
int l,r,ans=0,_min=2147483647,temp;
l=read(),r=read();
for(int i=l;i<=r;i+=getny(i))//每次枚举下一个末尾数字0的个数相同的数
if((temp=getnum(i))<_min){
_min=temp;
ans=i;
}
printf("%d\n",ans);
}
#ifdef DEBUG
while(1);
#endif
}