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?
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.
Very close, but not quite. For the state $$$n, 1$$$, you must account for doing the following for the next row:
for the state $$$n+1, 1$$$.
The transitions are as follows:
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.
Oh true. I was careless for the transitions going out from the state $$$n,1$$$.
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:
Yes, I just wanted to use the same notation that aniervs used, because most readers will probably read that first.