#P10297. Swipe Transformation
Swipe Transformation
Swipe Transformation
You are given two arrays \(A\) and \(B\), each of length \(N\). In one move you can perform one of the following sliding operations on \(A\):
- Right slide: Choose an interval \([l, r]\) (0-indexed) and set \(A_i = A_l\) for every \(l \le i \le r\).
- Left slide: Choose an interval \([l, r]\) (0-indexed) and set \(A_i = A_r\) for every \(l \le i \le r\).
Your task is to determine whether it is possible to transform array \(A\) into array \(B\) using a sequence of such operations. If it is possible, you should output any valid sequence of operations that does so.
The output format is as follows:
- If the transformation is impossible, output a single line containing
NO
. - If it is possible, output
YES
on the first line, followed by a line containing an integer \(K\) (the number of operations), and then \(K\) lines each describing an operation in the format:op l r
. Here,op
is eitherL
(for a left slide) orR
(for a right slide), and \(l\) and \(r\) (\(0 \le l \le r < N\)) are the indices defining the interval on which the operation is performed.
Hint: Notice that in a right slide, the left endpoint determines the value copied, while in a left slide the right endpoint determines the value. A natural strategy is to partition \(B\) into contiguous blocks of equal values and try to propagate the correct value to the entire block using at most two operations per block.
inputFormat
The first line contains a single integer \(N\) (the length of the arrays).
The second line contains \(N\) integers, the elements of array \(A\), separated by spaces.
The third line contains \(N\) integers, the elements of array \(B\), separated by spaces.
It is guaranteed that indices are 0-indexed.
outputFormat
If the transformation is impossible, output a single line containing NO
.
If it is possible, output the following:
- First line:
YES
- Second line: an integer \(K\) representing the number of operations.
- Next \(K\) lines: each line contains an operation in the format
op l r
, whereop
is eitherL
orR
, and \(l, r\) are the endpoints (0-indexed) of the interval.
sample
6
0 1 2 3 4 5
0 1 2 2 2 5
YES
1
R 2 4
</p>