Блог пользователя retry__

Автор retry__, история, 5 лет назад, По-английски

given a number x, we have to reach from 1 to x on number line and at each position i we can move to i+1 or to reverse of number i(ignoring the leading zeroes, say from 23 to 32). Find the minimum number of steps to reach x. x<=1e14

  • Проголосовать: нравится
  • -17
  • Проголосовать: не нравится

»
5 лет назад, # |
Rev. 3   Проголосовать: нравится 0 Проголосовать: не нравится
  • Hint $$$1$$$ The answer is $$$\lt 200$$$
Why ?
  • Hint $$$2$$$ BFS
How ?
  • Hint $$$3$$$ Greedy
How ?

P.S. — How do I did spoiler tags in my comment ?

»
5 лет назад, # |
Rev. 2   Проголосовать: нравится +1 Проголосовать: не нравится

let's start at x and move to 1 ,the best strategy will be always to move x%10 moves while you are greater than 10 then reverse ,as if you are dividing by 10 in x%10 moves but reversing the digits each time. let x=987, then you move 7 moves then reverse,now x=89 then move 9 moves then reverse , x=8 since you are smaller than 10 the best strategy is to move normally to 1 so the answer will be the sum of digits of x — 1 ,the minus 1 because we start from 1 not 0

  • »
    »
    5 лет назад, # ^ |
    Rev. 2   Проголосовать: нравится +11 Проголосовать: не нравится

    This is incorrect, because you can move from $$$980$$$ to $$$89$$$, but questions asks you to start from $$$1$$$ and go to $$$x$$$. You won't be able to move from $$$89$$$ to $$$980$$$, so this path is not reversible.