13 November

[HEOI2015] pricing

There are many on the market pricing of goods similar to 999 yuan, 4,999 yuan, 8,999 yuan so. They and 1000 yuan, 5000 yuan and 9000 yuan, and there is no essential difference, but in psychology will make people feel a lot cheaper, and therefore the price of common business strategy. However, in your opinion, this price is ridiculous. So you calculate such a ridiculous extent price p (p is a positive integer) of:

1, p is first seen as a string of digits (without the leading zero);
2, then, if the last character of p is 0, remove it. This process is repeated until the last character p is not 0;
3, p is referred to the length of a, at this time if p is the last bit 5, the degree of absurdity 2 * a - 1; otherwise 2 * a.

For example, the degree of absurdity is 3 850, and 880, compared with the extent of absurdity 4,9999 8.

Now, as you want to sell unused items, you can accept pricing within [L, R] range, the lowest price you want to give a ridiculous degree. T ≤ 100,1 ≤ L ≤ R ≤ 10 ^ 9.

greedy.

#include <cstdio>
#include <cstring>

int T, L, R;
int ans;

inline int calc(int x) {
    while (!(x%10)) x/=10;
    register int k=x%10, a=0;
    while (x) x/=10, ++a;
    if (k==5) return (a<<1)-1; return a<<1;
}
inline int add(int x) {
    register int k=1;
    while (!(x%10)) x/=10, k*=10;
    return k;
}

int main() {
    for (scanf("%d", &T); T; --T) {
        scanf("%d%d", &L, &R);
        ans=L;
        register int p=0x3f3f3f3f, q;
        while (L<=R) {
            q=calc(L);
            if (p>q) p=q, ans=L;
            L+=add(L);
        }
        printf("%d\n", ans);
    }
    return 0;   
}

Guess you like

Origin www.cnblogs.com/greyqz/p/11847322.html