codeforces-scrape

Codeforces scraped problems and editorials
git clone git://git.laack.co/codeforces-scrape.git
Log | Files | Refs | README

2247C.md (2309B)


      1 Source: https://codeforces.com/problemset/problem/2247/C
      2 
      3 C. Inversion of a Subsequence
      4 
      5 time limit per test
      6 
      7 2 seconds
      8 
      9 memory limit per test
     10 
     11 256 megabytes
     12 
     13 input
     14 
     15 standard input
     16 
     17 output
     18 
     19 standard output
     20 
     21 You are given two arrays $a$ and $b$ of length $n$, consisting only of $0$ and $1$.
     22 
     23 You may perform the following operation on $a$ any number of times:
     24 
     25 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.
     26 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.
     27 
     28 Find the minimum number of operations needed to transform $a$ into $b$, or determine that it is impossible.
     29 
     30 $^{\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.
     31 
     32 Input
     33 
     34 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.
     35 
     36 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$.
     37 
     38 The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($a_i \in \{0, 1\}$) — array $a$.
     39 
     40 The third line of each test case contains $n$ integers $b_1, b_2, \ldots, b_n$ ($b_i \in \{0, 1\}$) — array $b$.
     41 
     42 It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
     43 
     44 Output
     45 
     46 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.
     47 
     48 Example
     49 
     50 Input
     51 
     52 ```
     53 5
     54 
     55 1
     56 
     57 0
     58 
     59 0
     60 
     61 2
     62 
     63 1 0
     64 
     65 0 1
     66 
     67 3
     68 
     69 1 1 1
     70 
     71 0 0 0
     72 
     73 4
     74 
     75 1 0 1 0
     76 
     77 0 1 0 1
     78 
     79 5
     80 
     81 1 0 1 0 1
     82 
     83 1 1 1 1 1
     84 ```
     85 
     86 Output
     87 
     88 ```
     89 0
     90 
     91 1
     92 
     93 1
     94 
     95 2
     96 
     97 -1
     98 ```
     99 
    100 Note
    101 
    102 In the first example, $a = b$, so no operations are needed. Therefore, the answer is $0$.
    103 
    104 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$.