Luogu P4109 [HEOI2015]定价

版权声明:欢迎转载 https://blog.csdn.net/yandaoqiusheng/article/details/88746468

题目链接:传送门

显然
对相同位数的数字来说
后面的 0 0 越多答案越优
那我们按这个规则枚举
把这个位数的9种最优情况枚举完
再判断
就是思路的问题

/**
 * @Date:   2019-03-22T17:10:55+08:00
 * @Last modified time: 2019-03-22T17:19:36+08:00
 */
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <complex>
#include <algorithm>
#include <climits>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define A 1000010
#define B 2010

using namespace std;
typedef long long ll;
int T, l, r, we[A];
int work(int x) {
    while (x % 10 == 0) x /= 10;
    int len = int(log10(x)) + 1;
    return x % 10 == 5 ? len * 2 - 1 : len * 2;
}
int button(int x, int cnt = 0) {
    while (x % 10 == 0) {
        x /= 10;
        cnt++;
    }
    return cnt;
}

int main(int argc, char const *argv[]) {
    scanf("%d", &T); we[0] = 1;
    for (int i = 1; i < 10; i++) we[i] = we[i - 1] * 10;
    while (T--) {
        int ans = 0, minn = 0x3f3f3f3f;
        scanf("%d%d", &l, &r);
        for (int i = l; i <= r; i += we[button(i)]) {
            int x = work(i);
            if (minn > x) minn = x, ans = i;
        }
        printf("%d\n", ans);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/yandaoqiusheng/article/details/88746468