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
About Issues

We understand that our problem archive is not perfect. If you find any issues with the problem, including the statement, scoring configuration, time/memory limits, test cases, etc.

You may use this form to submit an issue regarding the problem. A problem moderator will review your issue and proceed it properly.

STOP! Before you submit an issue, please READ the following guidelines:

  1. This is not a place to publish a discussion, editorial, or requests to debug your code. Your issue will only be visible by you and problem moderators. Other users will not be able to view or reply your issues.
  2. Do not submit duplicated issues. If you have already submitted one, please wait for an moderator to review it. Submitting multiple issues will not speed up the review process and might cause your account to be banned.
  3. Issues must be filed in English or Chinese only.
  4. Be sure your issue is related to this problem. If you need to submit an issue regarding another problem, contest, category, etc., you should submit it to the corresponding page.

Active Issues 0

No issues in this category.

Closed/Resolved Issues 0

No issues in this category.