AtCoder Regular Contest 156
A
Non-Adjacent Flip
AtCoder
⌀ 510
原题
←B →
翻译中…

Problem Statement

翻译中…

We have $N$ coins numbered $1$ to $N$ with two distinguishable sides. A string $S$ represents the current state of the coins. If the $i$-th character of $S$ is 1, coin $i$ is showing heads; if that character is 0, coin $i$ is showing tails.

翻译中…

You can repeat the following operation zero or more times.

翻译中…
  • Choose a pair of integers $(i,j)$ such that $1\leq i < j\leq N$ and $j-i\geq \bm{2}$. Flip coin $i$ and coin $j$.
翻译中…

Determine whether it is possible to make all the $N$ coins show tails. If it is possible, find the minimum number of operations needed.

翻译中…

You are given $T$ test cases to solve.

翻译中…

Constraints

翻译中…
  • $1 \leq T \leq 2\times 10^5$
  • $3 \leq N \leq 2\times 10^5$
  • $S$ is a string of length $N$ consisting of 0 and 1.
  • All numbers in the input are integers.
  • For each input file, the sum of $N$ over the test cases is at most $2\times 10^5$.
翻译中…

Input

翻译中…

The input is given from Standard Input in the following format:

翻译中…

T
\mathrm{case}_1
\vdots
\mathrm{case}_T

翻译中…

​ Each case is in the following format:

翻译中…

N
S

翻译中…

Output

翻译中…

Print $T$ lines. The $i$-th line $(1\leq i \leq T)$ should contain the minimum number of operations needed to make all the coins show tails if it is possible, and -1 otherwise.

翻译中…

样例

Input #1
5
3
101
6
101101
5
11111
6
000000
30
111011100110101100101000000111
Output #1
1
2
-1
0
8
For the first test case, you can perform the operation with $(i,j)=(1,3)$ to make all the coins show tails in one operation. For the second test case, you can perform the operation with $(i,j)=(1,3)$ and then with $(i,j)=(4,6)$ to make all the coins show tails in two operations. For the third test case, you can prove that there is no way to make all the coins show tails, so you should print `-1`. For the fourth test case, the coins already show tails, so no operation is needed.
题目信息
题号
arc156_a
时间限制
2 s
内存限制
1024 MB

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