#K59407. Shortest Journey on a Circular Route

    ID: 30857 Type: Default 1000ms 256MiB

Shortest Journey on a Circular Route

Shortest Journey on a Circular Route

You are given a circular route with n stops, and a list of stop identifiers that appear along the route in order. Although the identifiers are provided, your task is to compute the minimum number of stops required to travel from a starting stop s to a destination stop t on this circular route.

The journey can be taken in either clockwise or counter-clockwise direction. The indices s and t are 1-based. Compute both distances and return the minimum.

Note: The list of stop identifiers is not used in the computation, but is included as part of the input.

Mathematical Formulation:

Convert the 1-based indices to 0-based as follows:

\[ s' = s - 1, \quad t' = t - 1 \]

Then compute the distances:

\[ \text{clockwise} = \begin{cases} t' - s' & \text{if } t' \geq s' \\ n - s' + t' & \text{if } t' < s' \end{cases} \] \[ \text{counterclockwise} = \begin{cases} s' - t' & \text{if } s' \geq t' \\ s' + n - t' & \text{if } s' < t' \end{cases} \]

The answer is the minimum of these two distances.

inputFormat

The input is read from standard input (stdin) and consists of multiple test cases. The format is as follows:

  1. The first line contains a single integer T indicating the number of test cases.
  2. For each test case:
    1. An integer n representing the total number of stops.
    2. A line with n space-separated integers representing the stop identifiers (this list is not used in the calculation).
    3. A line with two space-separated integers s and t representing the start and destination stops (1-based indices).

outputFormat

For each test case, output a single line to standard output (stdout) containing the minimum number of stops required to travel from stop s to stop t.

## sample
4
5
3 5 2 7 4
2 4
4
1 2 3 4
4 1
3
3 2 1
1 3
6
1 2 3 4 5 6
3 6
2

1 1 3

</p>