#P11643. Mio's Sequence Transformation
Mio's Sequence Transformation
Mio's Sequence Transformation
Mio has a sequence of n positive integers \(a_1, a_2, \ldots, a_n\). She is allowed to perform the following operation any number of times: choose two indices \(l, r\) such that \(1 \le l \le r \le n\) and the length \(r-l+1\) is even, and then either:
- For every \(i\) with \(l \le i \le r\), set \[ a_i \gets a_i + \Bigl(\left|i - \frac{r+l}{2}\right| + \frac{1}{2}\Bigr), \]
- or for every \(i\) with \(l \le i \le r\), set \[ a_i \gets a_i - \Bigl(\left|i - \frac{r+l}{2}\right| + \frac{1}{2}\Bigr). \]
These operations can be visualized as selecting a symmetric interval (with an even number of elements) and then adding or subtracting a "V‐shaped" pattern to the numbers in the interval. For example, if \(l=1\) and \(r=8\) then the pattern is \(4, 3, 2, 1, 1, 2, 3, 4\) (since \(\left|i-4.5\right|+0.5\) equals these values for \(i=1,2,\dots,8\)).
Throughout the process, Mio requires that all elements remain positive integers. Given the initial sequence \(a_1, \ldots, a_n\) and a target sequence \(b_1, \ldots, b_n\) (all positive integers), determine if it is possible to transform \(a\) into \(b\) by a sequence of such operations.
Hint: Note that each allowed operation keeps one invariant. In fact, if you define \(d_i=b_i-a_i\) then one may prove that
[ \sum_{i=1}^{n} (-1)^{i-1}(b_i-a_i)=0 ]
is a necessary and sufficient condition for the transformation to be possible.
inputFormat
The input begins with an integer \(n\) (\(1 \le n \le 10^5\)). The second line contains \(n\) positive integers \(a_1, a_2, \ldots, a_n\). The third line contains \(n\) positive integers \(b_1, b_2, \ldots, b_n\).
outputFormat
Output a single line containing either YES
if it is possible to transform sequence \(a\) into \(b\) using the allowed operations while keeping all numbers positive, or NO
otherwise.
sample
2
1 1
2 2
YES