您的位置:首页 > 其它

BZOJ 4029 [HEOI 4029] 定价 解题报告

2015-05-02 11:41 369 查看
这个题好像也是贪心的感觉。。

我们枚举 $1,5,10,50,100,\dots$ ,找出在 $[l, r]$ 内能整除它们的最小的数。

然后找到其中在荒谬值最小的情况下数值最小的那个数,

就做完了。

#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long LL;
#define INF 593119681

int _;
LL l, r;

inline LL Calc(LL x)
{
if (!~x) return INF;
while (x % 10 == 0) x /= 10;
int f = (x % 5) ? 0 : -1;
int res = 0;
for (; x; x /= 10)
res += 2;
return res + f;
}

int main()
{
#ifndef ONLINE_JUDGE
freopen("4029.in", "r", stdin);
freopen("4029.out", "w", stdout);
#endif

for (scanf("%d", &_); _; _ --)
{
scanf("%lld%lld", &l, &r);
LL best = -1, top, bot;
for (LL x = 1, _x = 5; x <= r; x *= 10, _x *= 10)
{
top = (l + x - 1) / x * x;
bot = r / x * x;
if (top <= bot) best = (Calc(best) < Calc(top) || (Calc(best) == Calc(top) && best < top)) ? best : top;
top = (l + _x - 1) / _x * _x;
bot = r / _x * _x;
if (top <= bot) best = (Calc(best) < Calc(top) || (Calc(best) == Calc(top) && best < top)) ? best : top;
}
printf("%lld\n", best);
}

#ifndef ONLINE_JUDGE
fclose(stdin);
fclose(stdout);
#endif
return 0;
}


4029_Gromah
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: