[lnsyoj4029/luoguP4109/HEOI2015]定价

题意

xx 去除后导零的值,则定义 f(x)=2(log10x+1)[x5(mod10)],给定区间 [L,R],求该区间中最小的 f(x) 值。

sol

一道贪心题,思想比较好想,我们需要使得前面的非 0 数字部分长度最小,且末尾尽可能为 5。具体实现中,我们可以运用倍增思想,向后枚举时使后面的 0 的部分长度不降,每次枚举判断其 f(x) 值,这样就可以 O(logR) 地枚举,总时间复杂度 O(log2R)

代码

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>

using namespace std;

const int K = 15;

int T;
int l, r;
int p[K];

int count(int x){
    int ans = 0;
    while (!(x % 10)) ans ++ , x /= 10;
    return ans;
}

int calc(int x){
    while (!(x % 10)) x /= 10;
    int ans = 0;
    if (x % 10 == 5) ans -- ;
    while (x) ans += 2 , x /= 10;
    return ans ;
}

int main(){
    p[0] = 1;
    for (int i = 1; i < 10; i ++ ) p[i] = p[i - 1] * 10;

    scanf("%d", &T);
    while (T -- ){
        scanf("%d%d", &l, &r);
        int ans = 0x3f3f3f3f, id;
        for (; l <= r; l += p[count(l)]){
            int t = calc(l);
            if (t < ans) ans = t, id = l;
        }
        printf("%d\n", id);
    }

    return 0;
}
posted @   是一只小蒟蒻呀  阅读(5)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 使用C#创建一个MCP客户端
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示