Codeforces Round 1019 (Div. 2)
B
Binary Typewriter
Codeforces
1100
原题
← AC →
翻译中…

You are given a binary string $s$ of length $n$ and a typewriter with two buttons: 0 and 1. Initially, your finger is on the button 0. You can do the following two operations:

翻译中…
  1. Press the button your finger is currently on. This will type out the character that is on the button.
  2. Move your finger to the other button. If your finger is on button 0, move it to button 1, and vice versa.
翻译中…

The cost of a binary string is defined as the minimum number of operations needed to type the entire string.

翻译中…

Before typing, you may reverse at most one substring$^{\text{∗}}$ of $s$. More formally, you can choose two indices $1\le l\le r\le n$, and reverse the substring $s_{l\ldots r}$, resulting in the new string $s_1s_2\ldots s_{l-1}s_rs_{r-1}\ldots s_ls_{r+1}\ldots s_n$.

翻译中…

Your task is to find the minimum possible cost among all strings obtainable by performing at most one substring reversal on $s$.

翻译中…

$^{\text{∗}}$A string $a$ is a substring of a string $b$ if $a$ can be obtained from $b$ by the deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end.

翻译中…

Input

翻译中…

Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows.

翻译中…

The first line of each test case contains a single integer $n$ ($1\le n\le 2\cdot 10^5$) — the length of the binary string $s$.

翻译中…

The second line of each test case contains a binary string $s_1s_2\ldots s_n$ ($s_i = \mathtt{0}$ or $s_i = \mathtt{1}$) — the characters of the binary string $s$.

翻译中…

It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.

翻译中…

Output

翻译中…

For each test case, output the minimum cost of string $s$ after performing at most one substring reversal.

翻译中…

Note

翻译中…

In the first test case, we can choose not to reverse any substrings. We can do operation $1$ three times to type 000.

翻译中…

In the second test case, we can choose not to reverse any substrings. We can do operation $2$ to move our finger to button 1. Then, we do operation $1$ three times to type 111.

翻译中…

In the third test case, we can choose not to reverse any substring. We can do operation $1$ to type 0. Then, we do operation $2$ to move our finger to button 1. Finally, we do operation $1$ two times to type 11, resulting in the final string 011 using only $4$ operations.

翻译中…

In the fourth test case, we can reverse the substring $s_{1\ldots 3}$, resulting in the string 001. We can do operation $1$ two times to type 00. Then we do operation $2$ to move our finger to button 1. Finally, we do operation $1$ once to type 1, resulting in the final string 001 using only $4$ operations.

翻译中…

In the fifth test case, we can reverse the substring $s_{2\ldots 3}$, resulting in the string 11001. The cost of the string is $8$ as we can do the following sequence of operations:

翻译中…
  • Do operation $2$ to move our finger to button 1.
  • Do operation $1$ two times to type 11.
  • Do operation $2$ to move our finger to button 0.
  • Do operation $1$ two times to type 00.
  • Do operation $2$ to move our finger to button 1.
  • Do operation $1$ once to type 1.
翻译中…

In the sixth test case, we can reverse the substring $s_{5\ldots 17}$, resulting in the string 1101111011001001000. It can be proven that the minimum number of operations needed to type the binary string is $29$.

翻译中…

样例

Input #1
6
3
000
3
111
3
011
3
100
5
10101
19
1101010010011011100
Output #1
3
4
4
4
8
29
InputCopy OutputCopy
题目信息
题号
2103B
时间限制
1.5 s
内存限制
256 MB
标签
贪心
数学

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。