#P3460. [POI 2007] TET-Tetris Attack
[POI 2007] TET-Tetris Attack
Description
A puzzle game named Tetris Attack is popular in Byteotia. The game itself is very complex, so we only describe its simplified rules:
The player has a stack with elements, one placed atop another. There are distinct symbols in total. For each symbol, exactly two elements in the stack are labeled with that symbol.
The player may swap two adjacent elements, i.e., exchange their positions. After a swap, if two adjacent elements bear the same symbol, remove both from the stack. Then all elements above them fall down, which may trigger further removals.
The goal is to empty the stack with the fewest moves. Write a program to find the minimum number of moves and one corresponding sequence of swaps.
Input Format
The first line contains an integer .
The next lines give the initial contents of the stack. The -th line contains an integer (), indicating that the -th element from the bottom is labeled with symbol (each symbol appears in the stack exactly twice).
Initially, there are no two adjacent elements with the same symbol. It is guaranteed that there exists a solution with at most moves.
Output Format
The first line contains an integer , the minimum number of moves.
The next lines each contain one number.
On the -th line, output , meaning that on the -th step the player swaps positions and .
If there are multiple valid solutions, output any of them.
5
5
2
3
1
4
1
4
3
5
2
2
5
2
Hint
.
Translated by ChatGPT 5
京公网安备 11011102002149号