Codeforces Round 1111 (Div. 2)
C
子序列反转
Inversion of a Subsequence
Codeforces
1100
原题
← BD1 →

子序列反转AI (CI翻译)

You are given two arrays $a$ and $b$ of length $n$, consisting only of $0$ and $1$.

给定两个长度为 $n$ 的数组 $a$ 和 $b$,其中只包含 $0$ 和 $1$。AI (CI翻译)

You may perform the following operation on $a$ any number of times:

你可以对 $a$ 执行任意次以下操作:AI (CI翻译)

  1. Choose $k$ indices $1 \le i_1 \lt i_2 \lt \ldots \lt i_k \le n$, where $1 \le k \le n$ and $\sum\limits_{j = 1}^k a_{i_j}$ is odd. In other words, choose a non-empty subsequence$^{\text{∗}}$ of $a$ with an odd sum.
  2. For each $1 \le j \le k$, set $a_{i_j} = 1 - a_{i_j}$. In other words, invert all elements of the chosen subsequence.
  1. 选择 $k$ 个下标 $1 \le i_1 \lt i_2 \lt \ldots \lt i_k \le n$,其中 $1 \le k \le n$ 且 $\sum\limits_{j = 1}^k a_{i_j}$ 为奇数。换句话说,选择 $a$ 的一个和为奇数的非空子序列$^{\text{∗}}$。
  2. 对于每个 $1 \le j \le k$,令 $a_{i_j} = 1 - a_{i_j}$。换句话说,将所选子序列中的所有元素取反。
AI (CI翻译)

Find the minimum number of operations needed to transform $a$ into $b$, or determine that it is impossible.

请找出将 $a$ 变为 $b$ 所需的最少操作次数,或者判断是否无法完成转换。AI (CI翻译)

$^{\text{∗}}$A sequence $c$ is a subsequence of a sequence $d$ if $c$ can be obtained from $d$ by the deletion of several (possibly, zero or all) elements from arbitrary positions.

$^{\text{∗}}$如果一个序列 $c$ 可以通过从序列 $d$ 的任意位置删除若干个(可能为零个或全部)元素得到,则称 $c$ 是 $d$ 的子序列。AI (CI翻译)

Input

输入格式

AI (CI翻译)

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.

每个测试包含多个测试用例。第一行包含测试用例的数量 $t$($1 \le t \le 10^4$)。接下来是各测试用例的描述。AI (CI翻译)

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

每个测试用例的第一行包含一个整数 $n$($1 \le n \le 2 \cdot 10^5$)——数组 $a$ 和 $b$ 的长度。AI (CI翻译)

The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($a_i \in \{0, 1\}$) — array $a$.

每个测试用例的第二行包含 $n$ 个整数 $a_1, a_2, \ldots, a_n$($a_i \in \{0, 1\}$)——数组 $a$。AI (CI翻译)

The third line of each test case contains $n$ integers $b_1, b_2, \ldots, b_n$ ($b_i \in \{0, 1\}$) — array $b$.

每个测试用例的第三行包含 $n$ 个整数 $b_1, b_2, \ldots, b_n$($b_i \in \{0, 1\}$)——数组 $b$。AI (CI翻译)

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

保证所有测试用例中 $n$ 的总和不超过 $2 \cdot 10^5$。AI (CI翻译)

Output

输出格式

AI (CI翻译)

For each test case, output a single integer — the minimum number of operations needed to transform $a$ into $b$, or $-1$ if it is impossible.

对于每个测试用例,输出一个整数——将 $a$ 变为 $b$ 所需的最少操作次数;如果无法完成转换,则输出 $-1$。AI (CI翻译)

Note

说明

AI (CI翻译)

In the first example, $a = b$, so no operations are needed. Therefore, the answer is $0$.

在第一个样例中,$a = b$,因此不需要执行任何操作,答案为 $0$。AI (CI翻译)

In the second example, we can perform an operation with the subsequence $[a_1, a_2]$. The sum of its elements is $1 + 0 = 1$, which is odd. This operation transforms $a$ as follows: $[\color{red}{1, 0}] \rightarrow [\color{red}{0, 1}]$. The resulting array equals $b$, so the answer is $1$.

在第二个样例中,我们可以选择子序列 $[a_1, a_2]$ 执行一次操作。其元素之和为 $1 + 0 = 1$,是奇数。该操作将 $a$ 变为:$[\color{red}{1, 0}] \rightarrow [\color{red}{0, 1}]$。所得数组等于 $b$,因此答案为 $1$。AI (CI翻译)

样例

Input #1
5
1
0
0
2
1 0
0 1
3
1 1 1
0 0 0
4
1 0 1 0
0 1 0 1
5
1 0 1 0 1
1 1 1 1 1
Output #1
0
1
1
2
-1
InputCopy OutputCopy
题目信息
题号
2247C
时间限制
2 s
内存限制
256 MB
标签
贪心
数学

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