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

Автор EugeneJudo, история, 3 года назад, По-английски

I worked through https://cses.fi/problemset/task/2413/, but i'm not too happy with my implementation: https://cses.fi/paste/a16f2727089e9e76291e5a/

It's very complicated, as I went with a route I saw would work, but knew would be hard to implement. Every integer represents one of the 20 possible row states, and dp figures out in the next row how many of type X appear based on the sum of the states that allow for type X to appear next. Looking at others solutions, no such enumeration is necessary, but I can't really understand why the alternative method works. Could someone explain the proper approach here?

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

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

I think the following approach is correct:

Let $$$f(n, 0)$$$ be the number of towers of height $$$n$$$ such that on the highest floor both columns are occupied by the same block; and $$$f(n, 1)$$$ the number of towers of height $$$n$$$ such that on the highest floor the columns are occupied by different blocks.

Then, from the state $$${n, 0}$$$ you can:

  • add a new block that covers both columns on top of the highest floor, leading to the state $$${n+1, 0}$$$.

  • extend the highest block to a new floor, leading to the state $$${n+1,0}$$$ too.

  • add one block for each column on top go the highest floor, leading to the state $$${n+1, 1}$$$.

And from the state $$${n, 1}$$$ you can:

  • add a new block that covers both columns on top of the highest floor, leading to the state $$${n+1, 0}$$$.

  • add one block for each column on top go the highest floor, leading to the state $$${n+1, 1}$$$.

I hope you can get the final transition recurrence formula. You should have no troubles once you understood everything I said above.

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

    Very close, but not quite. For the state $$$n, 1$$$, you must account for doing the following for the next row:

    • Old, Old
    • Old, New
    • New, Old
    • New, New

    for the state $$$n+1, 1$$$.

    The transitions are as follows:

    • $$$f(n, 0) = 2f(n+1,0) + f(n+1,1)$$$
    • $$$f(n, 1) = f(n+1,1) + 4f(n+1,0)$$$

    Implementation note: It's easier to use $$$dp_{i,0}, dp_{i,1}$$$ to be the answers for $$$n = i$$$, transition from $$$n$$$ to $$$n + 1$$$ with the formulas above. And if you sort the input, you can even solve it in $$$O(t)$$$ memory.

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

      Oh true. I was careless for the transitions going out from the state $$$n,1$$$.

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

      Thanks for the elaboration, the old/new phrasing made it very clear what's going on. I believe the states are mixed up in the second formula, since the 4 applies to different blocks at the top level. Also making n increase instead of decrease yields:

      • $$$f(n+1,0) = 2f(n,0) + f(n,1)$$$
      • $$$f(n+1,1) = f(n,0) + 4f(n,1)$$$
      • »
        »
        »
        »
        3 года назад, # ^ |
          Проголосовать: нравится +3 Проголосовать: не нравится

        Yes, I just wanted to use the same notation that aniervs used, because most readers will probably read that first.