CF2140A.Shift Sort

传统题 时间 2000 ms 内存 256 MiB 3 尝试 1 已通过 1 标签

Shift Sort

You are given a binary string^{\text{∗}} ss of length nn and you are allowed to perform the following operation any number of times (including zero):

  • Choose 33 indices 1i<j<kn1 \le i \lt j \lt k \le n and right shift or left shift the values on sis_i, sjs_j, sks_k cyclically.

For the binary string 110110, if we choose i=1i=1, j=2j=2, k=3k=3 and perform a right shift cyclically, the string becomes 011110; if we choose i=4i=4, j=5j=5, k=6k=6 and perform a left shift cyclically, the string becomes 110101.

Determine the minimum number of operations required to sort the given binary string.

^{\text{∗}}A binary string is a string that consists only of the characters 0 and 1.

Input

Each test contains multiple test cases. The first line contains the number of test cases tt (1t1001 \le t \le 100). The description of the test cases follows.

The first line of each test case contains a single integer nn (3n1003 \le n \le 100) — the length of the string.

The second line contains a binary string ss of length nn.

Output

For each test case, output a single integer — the minimum number of operations required to sort the given binary string.

Note

For the first test case, the given string is already sorted. So, no operations are needed.

For the second test case, we can choose i=1i = 1, j=2j = 2, k=4k = 4 and perform a right shift cyclically. The string becomes equal to 0011, which is sorted.

Samples

4
3
001
4
0110
6
110100
6
101011
0
1
2
1

在线编程 IDE

建议全屏模式获得最佳体验