#K40372. Rearranging Books
Rearranging Books
Rearranging Books
You are given N books arranged in a row. Each book has a height. You are also given a desired arrangement of these books. To rearrange the books, you are only allowed to swap two books if the absolute difference of their heights is at most K. In other words, two books with heights \(h_i\) and \(h_j\) can be swapped if and only if \(|h_i-h_j| \leq K\). Your task is to determine whether it is possible to transform the initial arrangement into the desired arrangement with a sequence of allowed swaps.
Note: The input guarantees that the collection of book heights in the initial arrangement and the desired arrangement are identical, though they may be arranged in different orders.
inputFormat
The input is read from standard input and has the following format:
N K h1 h2 ... hN d1 d2 ... dN
Here, N is the number of books, K is the maximum allowed height difference for a swap, the second line contains the initial arrangement of the books, and the third line contains the desired arrangement.
outputFormat
Output a single line to standard output containing either YES
if the rearrangement is possible under the given constraints, or NO
otherwise.
5 3
3 1 4 2 5
1 2 3 4 5
YES