CF2247C.Inversion of a Subsequence
入门
通过率:0%
时间限制:2.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
You are given two arrays a and b of length n, consisting only of 0 and 1.
You may perform the following operation on a any number of times:
- Choose k indices 1≤i1<i2<…<ik≤n, where 1≤k≤n and j=1∑kaij is odd. In other words, choose a non-empty subsequence∗ of a with an odd sum.
- For each 1≤j≤k, set aij=1−aij. In other words, invert all elements of the chosen subsequence.
Find the minimum number of operations needed to transform a into b, or determine that it is impossible.
∗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.
给你两个长度为 n 的数组 a 和 b,其中仅包含 0 和 1。
你可以对数组 a 执行以下操作任意多次:
- 选择 k 个下标 1≤i1<i2<…<ik≤n,其中 1≤k≤n,且 j=1∑kaij 为奇数。换言之,选择 a 的一个非空子序列∗,其元素和为奇数。
- 对每个 1≤j≤k,令 aij=1−aij。换言之,将所选子序列中所有元素取反。
求将 a 变为 b 所需的最少操作次数;若不可能实现,则判定为不可行。
∗ 序列 c 是序列 d 的子序列,当且仅当 c 可通过从 d 中删除若干(可能为零个或全部)任意位置的元素而得到。
输入格式
Each test contains multiple test cases. The first line contains the number of test cases t (1≤t≤104). The description of the test cases follows.
The first line of each test case contains a single integer n (1≤n≤2⋅105) — the length of the arrays a and b.
The second line of each test case contains n integers a1,a2,…,an (ai∈0,1) — array a.
The third line of each test case contains n integers b1,b2,…,bn (bi∈0,1) — array b.
It is guaranteed that the sum of n over all test cases does not exceed 2⋅105.
每个测试包含多个测试用例。第一行包含测试用例的数量 t(1≤t≤104)。随后是各测试用例的描述。
每个测试用例的第一行包含一个整数 n(1≤n≤2⋅105)—— 数组 a 和 b 的长度。
每个测试用例的第二行包含 n 个整数 a1,a2,…,an(ai∈{0,1})—— 数组 a。
每个测试用例的第三行包含 n 个整数 b1,b2,…,bn(bi∈{0,1})—— 数组 b。
保证所有测试用例中 n 的总和不超过 2⋅105。
输出格式
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。
输入输出样例
输入#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=b, so no operations are needed. Therefore, the answer is 0.
In the second example, we can perform an operation with the subsequence [a1,a2]. The sum of its elements is 1+0=1, which is odd. This operation transforms a as follows: [1,0]→[0,1]. The resulting array equals b, so the answer is 1.
在第一个例子中,a=b,因此无需执行任何操作。答案为 0。
在第二个例子中,我们可以对子序列 [a1,a2] 执行一次操作。该子序列元素之和为 1+0=1,是奇数。该操作将 a 变换如下:[1,0]→[0,1]。变换后的数组等于 b,因此答案为 1。
输入解题思路,AI测评打分。不知道怎么写?