#P9383. Card Elimination Game
Card Elimination Game
Card Elimination Game
You are given a deck of m cards and 2 stacks. The deck contains cards with k different patterns numbered from 1 to \(k\). The deck is arranged from top to bottom as \(a_1, a_2, \dots, a_m\) and it is guaranteed that \(m=2k\); that is, every card pattern appears exactly twice.
You can perform two kinds of operations:
- Operation 1: Choose one of the stacks and take the card from the top of the deck and push it onto the chosen stack. After the push, if the top two cards of that stack have the same pattern (i.e. the recently pushed card and the card immediately below it), then both cards are automatically removed from the stack.
- Operation 2: Choose two different stacks; if the top cards of the two stacks have the same pattern, then you may remove both cards from their stacks. (If they are different, nothing happens.)
Your goal is to design a sequence of operations so that all cards are eventually eliminated from both stacks.
Input Note: It is guaranteed that a valid sequence of operations exists for the given deck.
inputFormat
The first line contains an even integer \(m\) (\(m=2k\)). The second line contains \(m\) integers \(a_1, a_2, \dots, a_m\) representing the card patterns from the top to the bottom of the deck.
It is guaranteed that each card pattern (an integer between 1 and \(k\)) appears exactly twice.
outputFormat
First, output an integer \(t\) which is the number of operations in your sequence. Then output \(t\) lines; each line represents one operation in the order they are performed. An operation can be of one of the following formats:
1 x
: This means you perform Operation 1 by pushing the top card of the deck onto stack \(x\) (where \(x\) is either 1 or 2).2 1 2
: This means you perform Operation 2 by simultaneously removing the top card from both stacks (note that since there are only 2 stacks, the operation is always performed on stack 1 and stack 2).
Your output sequence must lead to both stacks being empty at the end.
sample
4
1 2 1 2
5
1 1
1 2
1 1
1 1
2 1 2
</p>