#K75312. Minimum Operations to Transform Array

    ID: 34392 Type: Default 1000ms 256MiB

Minimum Operations to Transform Array

Minimum Operations to Transform Array

You are given two arrays of integers: an initial array and a target array, both of length \(n\). Your task is to determine the minimum number of subarray reversal operations needed to transform the initial array into the target array.

An operation is defined as reversing any one continuous subarray. Formally, if you reverse the subarray spanning indices \(l\) to \(r\) (0-indexed), the transformation is:

[ a[l..r] \rightarrow a[r..l] ]

For example, if \(n = 5\), the initial array is \([1, 2, 3, 4, 5]\) and the target is \([5, 4, 3, 2, 1]\), one reversal (reversing the entire array) is sufficient.

If the arrays are already identical, then zero operations are required. In some cases, more than one reversal may be necessary.

inputFormat

The input is read from standard input (stdin) and consists of three lines:

  • The first line contains a single integer \(n\) representing the length of the arrays.
  • The second line contains \(n\) space-separated integers representing the initial array.
  • The third line contains \(n\) space-separated integers representing the target array.

outputFormat

Output a single integer to standard output (stdout): the minimum number of reversal operations required to transform the initial array into the target array.

## sample
5
1 2 3 4 5
5 4 3 2 1
1