#K59407. Shortest Journey on a Circular Route
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:
- The first line contains a single integer
T
indicating the number of test cases. - For each test case:
- An integer
n
representing the total number of stops. - A line with
n
space-separated integers representing the stop identifiers (this list is not used in the calculation). - A line with two space-separated integers
s
andt
representing the start and destination stops (1-based indices).
- An integer
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
.
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>