#P8866. Clearing the Cards in Meow Game

    ID: 22030 Type: Default 1000ms 256MiB

Clearing the Cards in Meow Game

Clearing the Cards in Meow Game

In this problem, you are given a deck of m cards and n stacks. The deck (from top to bottom) has cards with patterns \(a_1, a_2, \dots, a_m\). There are \(k\) distinct patterns identified by numbers from 1 to \(k\), and every pattern appears an even number of times in the deck.

You are allowed two types of operations:

  • Operation 1: Choose one stack and move the top card of the deck onto the top of that stack. Immediately after the operation, if the top two cards of this stack have the same pattern, they are automatically removed.
  • Operation 2: Choose two different stacks. If the bottom cards of both stacks have the same pattern, you can remove both bottom cards simultaneously. After removal, the card immediately above becomes the new bottom card of that stack. (If the bottom cards differ, nothing happens.)

The game consists of \(T\) levels. For each level, you are given the initial deck and the number of stacks. Your task is to design a sequence of operations that will remove all the cards from the game (i.e. clear the deck and all stacks become empty).

Note: The automatic removal in Operation 1 happens immediately when two identical cards become adjacent at the top of a stack. Also, the input guarantees that every card pattern appears an even number of times so that a solution exists.

inputFormat

The first line contains an integer \(T\) (\(T \ge 1\)) indicating the number of test cases. For each test case, the first line contains three integers \(n\), \(m\), and \(k\) where \(n\) is the number of stacks, \(m\) is the number of cards in the deck, and \(k\) is the number of distinct patterns. The next line contains \(m\) integers \(a_1, a_2, \dots, a_m\) representing the patterns on the cards from top to bottom.

Operation Format:
For Operation 1, output a line with: 1 x meaning you push the top card of the deck onto stack x (1-indexed).
For Operation 2, output a line with: 2 x y meaning you remove the bottom cards of stack x and stack y if they have the same pattern.

outputFormat

For each test case, first output an integer \(Q\) representing the number of operations. Then output \(Q\) lines, each representing an operation in the order they are performed. The operations must be such that after performing them (with automatic removals applied), all the cards are removed from the deck and all stacks are empty.

sample

1
2 4 2
1 2 1 2
4

1 1 1 2 1 1 1 2

</p>