#P14258. 好感(favor)

    ID: 13452 远端评测题 1000ms 512MiB 尝试: 0 已通过: 0 难度: 5 上传者: 标签>贪心2025洛谷原创O2优化洛谷月赛双指针 two-pointer

好感(favor)

Description

Little S and Little Y are using nn floating boards arranged in a row in the swimming pool. Each board is either face up or face down. They number the boards from front to back as 11 to nn.

Each time, Little S can choose a board and flip it forward. Due to the water flow generated by the flip, all boards in front of it, including itself, will move forward one position. Then, Little Y will move the originally frontmost float board back to the empty position without flipping it.

Specifically, suppose Little S chooses board ii. Then the boards in front of it are boards 1,,i1, \dots, i. After they all move forward one position, the original board jj will move to position j1j-1. In particular, the board now at position i1i-1 is the original board ii after being flipped, while the other boards are not flipped. After this, the original board 11 will move to position 00, and position ii will have an empty spot. Little Y will move this board from position 00 to position ii without flipping it, covering a distance of ii.

Little S wants to know how she should flip the boards so that, while making all boards face the same side up, the total distance Little Y moves the boards is minimized.

Input Format

This problem contains multiple test cases.

The first line of input contains a positive integer TT, representing the number of test cases.

This is followed by TT test cases, each formatted as follows:

The first line contains an integer nn, indicating the number of float boards.

The second line contains nn characters b1,,bnb_1, \dots, b_n, each of which can only be either 0\texttt{0} or 1\texttt{1}, indicating the initial state of the float boards. Here, bi=0b_i = \texttt{0} indicates that float board ii is facing up, and bi=1b_i = \texttt{1} indicates that it is facing down.

Output Format

For each test case, output a single integer in a line indicating the answer.

3
2
01
3
001
7
1011100

1
3
10

Hint

【Sample 1 Explanation】

For the first test case, after Little S flips float board 11, Little Y moves the float board from position 00 to 11. At this point, all float boards are facing down, and the total movement distance is 11.

For the second test case, after Little S flips float board 33, Little Y moves the float board from position 00 to 33. At this point, all float boards are facing up, and the total movement distance is 33.

For the third test case, they can proceed with the following steps:

  1. Little S flips float board 11, Little Y moves the float board from 00 to 11 (distance 11). The state of the float boards becomes 0011100.
  2. Little S flips float board 55, Little Y moves the float board from 00 to 55 (distance 55). The state becomes 0110000.
  3. Little S flips float board 33, Little Y moves the float board from 00 to 33 (distance 33). The state becomes 1000000.
  4. Little S flips float board 11, Little Y moves the float board from 00 to 11 (distance 11). The state becomes 0000000.

Finally, all float boards are facing up, with a total movement distance of 1+5+3+1=101+5+3+1=10. It can be proven that no solution with a smaller total movement distance exists.

【Sample 2】

See favor2.in and favor2.ans in the problem attachment.

This sample satisfies the special properties for test datas 5 and 6.

【Sample 3】

See favor3.in and favor3.ans in the problem attachment.

This sample satisfies the special properties for test datas 7 and 8.

【Data Range】

For all test data, it is guaranteed that: 1T51 \le T \le 5, 1n1061 \le n \le 10^6, and bi=0b_i = \texttt{0} or 1\texttt{1}.

::cute-table{tuack}

Test Data ID nn \le Special Property
1,21,2 1010 None
3,43,4 10310^3 Present
5,65,6 ^ None
7,87,8 10610^6 Present
9,109,10 ^ None

Special Property: There exists a 1kn11 \le k \le n-1 such that for 1ik1 \le i \le k, bi=0b_i = \texttt{0}; and for k+1ink+1 \le i \le n, bi=1b_i = \texttt{1}.