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

Автор viren0, 6 лет назад, По-английски

Can someone help in telling logic behind the logic of this code for this problem.

I found it difficult to implement brute force logic in Java.

Thanks

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

»
6 лет назад, # |
Rev. 3   Проголосовать: нравится -8 Проголосовать: не нравится

It is basically a Dynamic Programming solution.

This is my Accepted C++14 brute-force recursive solution, which may be more intuitive and simpler to comprehend.

  • »
    »
    6 лет назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    Can you elaborate your solution

    • »
      »
      »
      6 лет назад, # ^ |
      Rev. 10   Проголосовать: нравится -9 Проголосовать: не нравится

      The idea is to store the present state in a state_t object that consists of the triple:

      1. cookies: the present number of cookies

      2. S: the present number of cookies gained per second

      3. T: the present time in seconds

      The initial state is (0, S, 0), and C is the number of desired cookies.

      A factory is storied as a factory_t object that consists of the triple:

      1. cost: the cost of purchasing the factory

      2. production: the number of bonus cookies it produces when purchased

      3. bought: a boolean flag that indicates whether it has been bought or not

      Initially, all the N factories are not bought.

      The recursive factories_t::min_time( state_t x ) function is a brute-force recursive function that initializes min_time to the minimum number of seconds required so that the number of cookies reaches at least C according to the number of cookies and the gain per second in the present state without purchasing more factories.

      Then, the function iterates on all factories. If some factory[ i ] is not bought yet, then its corresponding delta is computed, which is the minimum time according to the present state such that it is possible to buy such factory. If such number is strictly positive, then the time the present state advanced by that duration. Then, the present state is updated according as the factory is purchased and the factory is marked as bought. The same function is then called recursively with the updated state to compute the minimum time for the update state.

      After returning back from the recursive call, min_time is updated, and the state is backtracked to its previous state so as to check if it is possible to update the minimum time to a lower value by buying another factory at that recursion level.