CF2247C.Inversion of a Subsequence

入门

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

You are given two arrays aa and bb of length nn, consisting only of 00 and 11.

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

  1. Choose kk indices 1≤i1<i2<…<ik≤n1 \le i_1 \lt i_2 \lt \ldots \lt i_k \le n, where 1≤k≤n1 \le k \le n and ∑j=1kaij\sum\limits_{j = 1}^k a_{i_j} is odd. In other words, choose a non-empty subsequence∗^{\text{∗}} of aa with an odd sum.
  2. For each 1≤j≤k1 \le j \le k, set aij=1−aija_{i_j} = 1 - a_{i_j}. In other words, invert all elements of the chosen subsequence.

Find the minimum number of operations needed to transform aa into bb, or determine that it is impossible.

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

给你两个长度为 nn 的数组 aa 和 bb,其中仅包含 00 和 11。

你可以对数组 aa 执行以下操作任意多次:

  1. 选择 kk 个下标 1≤i1<i2<…<ik≤n1 \le i_1 \lt i_2 \lt \ldots \lt i_k \le n,其中 1≤k≤n1 \le k \le n,且 ∑j=1kaij\sum\limits_{j = 1}^k a_{i_j} 为奇数。换言之,选择 aa 的一个非空子序列∗^{\text{∗}},其元素和为奇数。
  2. 对每个 1≤j≤k1 \le j \le k,令 aij=1−aija_{i_j} = 1 - a_{i_j}。换言之,将所选子序列中所有元素取反。

求将 aa 变为 bb 所需的最少操作次数;若不可能实现,则判定为不可行。

∗^{\text{∗}} 序列 cc 是序列 dd 的子序列,当且仅当 cc 可通过从 dd 中删除若干(可能为零个或全部)任意位置的元素而得到。

输入格式

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

The first line of each test case contains a single integer nn (1≤n≤2⋅1051 \le n \le 2 \cdot 10^5) — the length of the arrays aa and bb.

The second line of each test case contains nn integers a1,a2,…,ana_1, a_2, \ldots, a_n (ai∈0,1a_i \in {0, 1}) — array aa.

The third line of each test case contains nn integers b1,b2,…,bnb_1, b_2, \ldots, b_n (bi∈0,1b_i \in {0, 1}) — array bb.

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

每个测试包含多个测试用例。第一行包含测试用例的数量 tt(1≤t≤1041 \le t \le 10^4)。随后是各测试用例的描述。

每个测试用例的第一行包含一个整数 nn(1≤n≤2⋅1051 \le n \le 2 \cdot 10^5)—— 数组 aa 和 bb 的长度。

每个测试用例的第二行包含 nn 个整数 a1,a2,…,ana_1, a_2, \ldots, a_n(ai∈{0,1}a_i \in \{0, 1\})—— 数组 aa。

每个测试用例的第三行包含 nn 个整数 b1,b2,…,bnb_1, b_2, \ldots, b_n(bi∈{0,1}b_i \in \{0, 1\})—— 数组 bb。

保证所有测试用例中 nn 的总和不超过 2⋅1052 \cdot 10^5。

输出格式

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

对于每个测试用例,输出一个整数——将 aa 变换为 bb 所需的最少操作次数;如果无法实现,则输出 −1-1。

输入输出样例

  • 输入#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

    输出#1

    0
    1
    1
    2
    -1

说明/提示

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

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

在第一个例子中,a=ba = b,因此无需执行任何操作。答案为 00。

在第二个例子中,我们可以对子序列 [a1,a2][a_1, a_2] 执行一次操作。该子序列元素之和为 1+0=11 + 0 = 1,是奇数。该操作将 aa 变换如下:[1,0]→[0,1][\color{red}{1, 0}] \rightarrow [\color{red}{0, 1}]。变换后的数组等于 bb,因此答案为 11。

输入解题思路,AI测评打分。不知道怎么写?

首页