Editorial for DMOPC '21 Contest 1 P2 - Folding Clothes
Submitting an official solution before solving the problem yourself is a bannable offence.
Author:
There are a plethora of solutions to this problem, of which we present a few notable ideas. All the presented solutions can be comfortably implemented in . Feel free to add your own solutions in the comments.
moves
The most primitive solution. Similar to a selection sort algorithm, we will sort the clothes in decreasing order of width (i.e. bottom to top). Let's say we have already sorted the bottom clothes in the first stack, and we want to get the -th piece of clothing where it should be. First, we can use moves to move it to the top of the first stack, regardless of where it is currently:
- Move all clothes above the -th to the second stack.
- Move the -th piece of clothing to the second stack.
- Move everything in the second stack back to the first.
Then, we can use another moves to get it on top of the -th piece of clothing:
- Move all clothes above the -th to the second stack.
- Move the -th piece of clothing, which is now at the top of the second stack, back to the first.
- Move everything in the second stack back to the first.
It is easy to see that we use at most moves per item of clothing, so there are a total of moves. This scores us points.
moves
Let's optimize the strategy above. Again, we sort the clothes in decreasing order of width. If we have already sorted the bottom clothes in the first stack, then to get the -th piece of clothing where it should be:
- Move all clothes above the -th to the second stack.
- Move all clothes above and including the -th back to the first stack.
- Move everything in the second stack back to the first.
Now we use at most moves per item of clothing, so there are a total of moves. This scores us points.
moves: First Strategy
We can further improve upon the solution, by noting that it is not necessary to return all the clothes back to the first stack every single time. Thus, we may save the last move from every iteration and proceed as follows:
- Move all clothes above the -th to the second stack.
- Move all clothes above and including the -th back to the first stack.
This leaves some clothes in the second stack temporarily, but everything should be back in place (due to the nature of the algorithm) in the end.
Since we use at most moves per item of clothing, there are a total of moves maximum. This is enough for points.
moves: Second Strategy
We may apply the logic of insertion sort to this problem. We maintain a sorted stack of clothes in the second stack, and each time insert the top piece of clothing in the first stack to where it should be in the second. First, let's move the top piece of clothing in the first stack to the second. Now, for the next clothes, denote the piece of clothing currently at the top of the first stack :
- Move all clothes with smaller width than in the second stack back on top of in the first stack. We know this will be a prefix of the second stack since we maintain its sorted property.
- Move all clothes above and including back to the second stack.
In this manner, we have inserted to the position where it should be in the second stack. Finally, we move the fully sorted second stack back to the first. This algorithm takes moves at maximum, so it is also sufficient for points.
Comments