#P14258. 好感(favor)
好感(favor)
Description
Little S and Little Y are using 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 to .
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 . Then the boards in front of it are boards . After they all move forward one position, the original board will move to position . In particular, the board now at position is the original board after being flipped, while the other boards are not flipped. After this, the original board will move to position , and position will have an empty spot. Little Y will move this board from position to position without flipping it, covering a distance of .
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 , representing the number of test cases.
This is followed by test cases, each formatted as follows:
The first line contains an integer , indicating the number of float boards.
The second line contains characters , each of which can only be either or , indicating the initial state of the float boards. Here, indicates that float board is facing up, and 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 , Little Y moves the float board from position to . At this point, all float boards are facing down, and the total movement distance is .
For the second test case, after Little S flips float board , Little Y moves the float board from position to . At this point, all float boards are facing up, and the total movement distance is .
For the third test case, they can proceed with the following steps:
- Little S flips float board , Little Y moves the float board from to (distance ). The state of the float boards becomes
0011100. - Little S flips float board , Little Y moves the float board from to (distance ). The state becomes
0110000. - Little S flips float board , Little Y moves the float board from to (distance ). The state becomes
1000000. - Little S flips float board , Little Y moves the float board from to (distance ). The state becomes
0000000.
Finally, all float boards are facing up, with a total movement distance of . 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: , , and or .
::cute-table{tuack}
| Test Data ID | Special Property | |
|---|---|---|
| None | ||
| Present | ||
| ^ | None | |
| Present | ||
| ^ | None |
Special Property: There exists a such that for , ; and for , .
京公网安备 11011102002149号