Puzzle: Put a Piece on Square 1! Take a Piece off Square 2!

Bubbler @bubbler@hackers.pub
Problem
You have a game board with 20 empty squares arranged horizontally and 20 game pieces. Each empty square is numbered from 1 to 20, starting from the left. You can perform the following actions as many times as you want:
- You can place a piece on square 1 or remove a piece from square 1.
- If there is at least one piece on the board, let's call
the smallest number of a square that has a piece on it. Then you can place a piece on square or remove a piece from square .
Is it possible to have pieces on all squares? If so, what is the minimum number of actions required?
Source: The problem content was modified into a puzzle format from BOJ 29225. Пещеры.
Solution
Spoiler
To fill all squares, we first need to place a piece on square 20. To place a piece on square 20, we first need to place a piece on square 19, and... to place a piece on square 2, we first need to place a piece on square 1.Let's figure out how many actions are needed to place a piece on square k+
and removing a piece as k-
.
- Square 2:
1+ 2+
- Square 3:
1+ 2+ 1- 3+
- Square 4:
1+ 2+ 1- 3+ 1+ 2- 1- 4+
- Square 5:
1+ 2+ 1- 3+ 1+ 2- 1- 4+ 1+ 2+ 1- 3- 1+ 2- 1- 5+
From this pattern, we can infer that the number of actions needed to place a piece on square
To explain more logically: to place a piece on square
Now, returning to the original problem, the number of actions needed to place a piece on square 20 is