#P5918. [FJOI2015] 金币换位问题

[FJOI2015] 金币换位问题

Problem Description

Given an integer nn, the initial sequence looks like this:

$$\underbrace{\tt 111\cdots11}_{n \text{ ones}}\underbrace{\tt 000\cdots00}_{n \text{ zeros}}\verb!__!$$

Now you need to use the minimum number of swaps to make the final sequence become:

$$\underbrace{\tt 101010\cdots 1010}_{2\times n \text{ digits with alternating } 0 \text{ and } 1}\verb!__!$$

A swap means moving two adjacent non-blank digits together onto the two blanks.

For example, the following is a valid solution when n=4n=4:

  • Initial state: 11110000__\verb!11110000__!.
  • Step 11: __11000011\verb!__11000011!.
  • Step 22: 101__00011\verb!101__00011!.
  • Step 33: 1010100__1\verb!1010100__1!.
  • Step 44: 10101__001\verb!10101__001!.
  • Step 55: 10101010__\verb!10101010__!.

It can be proven that the minimum number of operations is 55.

Input Format

The input contains one line with an integer nn.

Output Format

Output the minimum number of moves in the first line.

In the next line, output a move plan. You only need to output the index of the left one of the two moved non-blank cells. See the sample for details.

4
5
1 4 8 6 9

Hint

For 100%100\% of the testdata, 2<n2×1052<n\le 2\times 10^5.

Translated by ChatGPT 5