#P3460. [POI 2007] TET-Tetris Attack

    ID: 2515 远端评测题 1000ms 125MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>贪心2007树状数组POISpecial Judge

[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 2n2n elements, one placed atop another. There are nn 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 nn.

The next 2n2n lines give the initial contents of the stack. The (i+1)(i+1)-th line contains an integer aia_i (1ain1 \leq a_i \leq n), indicating that the ii-th element from the bottom is labeled with symbol aia_i (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 10610^6 moves.

Output Format

The first line contains an integer mm, the minimum number of moves.

The next mm lines each contain one number.

On the (i+1)(i+1)-th line, output pip_i, meaning that on the ii-th step the player swaps positions pip_i and pi+1p_{i+1}.

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

1n500001 \le n \le 50000.

Translated by ChatGPT 5