QOJ.ac

QOJ

Time Limit: 1 s Memory Limit: 32 MB Total points: 100

#12694. Tetris Attack

统计

A puzzle called "Tetris Attack" has lately become a very popular game in Byteotia. The game itself is highly sophisticated, so we shall only introduce its simplified rules: the player is given a stack of $2n$ elements, placed one on another and marked with n different symbols. Exactly two elements of the stack are marked with each symbol. A player's move consists in swapping two neighbouring elements, ie. interchanging them. If, as a result of the swap, there are some two neighbouring elements marked with the same symbol, they are both removed from the stack. Then all the elements that have been above them fall down in consequence and may very well cause another removals. The player's goal is emptying the stack in the least possible number of moves.

Write a programme that:

  • reads the description of the initial stack content from the standard input,
  • finds a solution with the minimum number of moves possible,
  • writes out the outcome to the standard output.

Input

In the first line of the standard input there is one integer $n$, $1 ≤ n ≤ 50,000$. The following $2n$ lines describe the initial content of the stack. The $(i+1)$’th line contains one integer $a_i$ - the symbol which the $i$’th ($1 ≤ a_i ≤ n$) element from the bottom is marked with. Each symbol appears in the stack exactly twice. Moreover, no two identical symbols neighbour initially. The test data is well chosen so that a solution with no more than $1\,000\,000$ moves exists.

Output

A solution with the minimum number of moves possible should be written out to the standard output as follows. The first line should contain one integer $m$ - the minimum number of moves. The following $m$ should describe the optimal solution itself, i.e. a sequence of $m$ integers $p_i,…,p_m$, one in each line. $p_i$ denotes that in $i$’th move the player has chosen to swap the pi’th and $(p_i+1)$’th elements from the bottom.

If more than one optimal solution exists, your programme should write out any one of them.

Example

Input

5
5
2
3
1
4
1
4
3
5
2

Output

2
5
2
problem_12694_1.gif

Input

3
1
2
3
1
2
3

Output

3
3
4
2
problem_12694_2.gif

Alternative Output

3
4
3
2
problem_12694_3.gif

Discussions

About Discussions

The discussion section is only for posting: Editorials, General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues. Submitting multiple issues may cause your account to be banned.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.