P13816 [CERC 2022] The Game
Description
Vladimir is the loneliest child in the neighbourhood. No other kid likes to play with him. His parents decided to cheer him up so they bought him a card game called _The Game_. This card game is for up to 5 players, but it can also be played in the _solo_ (i.e. single-player) mode.
The package contains 98 _regular_ playing cards that are labeled by integers $2, 3, \ldots, 99$. In addition to these, there are 4 special _direction_ cards. Two of them are labeled with the number $1$ (followed by an up arrow) and the other two are labeled with $100$ (followed by a down arrow).
In the initial phase of the game, the pile of regular cards is shuffled and placed face down on the table – this will be the _draw pile_. The four direction cards are placed in a column; the two cards labeled $1$ have to be at the top. There should also be enough space on the right-hand side of each direction card – this is where regular cards will be laid during the play. The card labeled $1$ initiates an _ascending row_, while a card labeled $100$ initiates a _descending row_. In the solo mode, the player draws the top 8 cards from the draw pile, one by one, and puts them in his hand.
After the initial phase the game starts. On each turn the player has to play two cards from his hand according to the following rules:
- A card may be placed at the end of an ascending row if it is larger than the last (i.e. right-most) card in the row.
- A card may be placed at the end of a descending row if it is smaller than the last card in the row.
- A card with a smaller label may be placed at the end of an ascending row, or a card with a larger label may be placed at the end of a descending row, if the absolute difference between its value and the value of the last card in the row is exactly $10$. This move is called the _backwards trick_. Note that because of this extra rule, the values of the cards in an ascending row are not necessarily ascending (and similarly, the values of the cards in a descending row are not necessarily descending).
After playing two cards from the hand, the player should draw two new cards from the draw pile, one by one. This concludes his turn. If the draw pile is empty, he continues playing in the same way without drawing new cards. The game ends when the player has no cards left in his hand (in that case the player _beats the game_) or if he cannot play any of the remaining cards in his hand (in that case the player has _lost the game_).
**Example:** Suppose that the player's initial hand (i.e. the first 8 cards which he has drawn) is:
69, 17, 59, 32, 31, 77, 87, 89
He may decide to play the card 89 (putting it in the first descending row) and the card 17 (putting it in the second ascending row). The state of all four rows after the move is:
```
1 -> 17
1 ->
100 17
100
Input Format
The first (and only) line of the input contains 98 space-separated integers, i.e. some permutation of the set $\{2, 3, \ldots, 99\}$ that represents the initial draw pile. The cards are listed in order from top to bottom of the draw pile.
Output Format
The output contains six lines. The first four lines describe the four rows of cards on the table. The fifth line lists the cards that remained in the player's hand (if any) while the last line lists the cards that remained in the draw pile (if any). Print an empty line in case of an empty list. Cards in the four rows and in the hand should be ordered from left to right, while the cards in the last line, which represents the remainder of the draw pile, should be ordered from top to bottom as in the input data. See also the sample outputs.