#K42447. Minimum Posts Traversal in Circular Layout

    ID: 27089 Type: Default 1000ms 256MiB

Minimum Posts Traversal in Circular Layout

Minimum Posts Traversal in Circular Layout

You are given a set of posts arranged in a circular layout. Each post is associated with an angle (in degrees) and is labeled sequentially from 1 to \( n \) in clockwise order. Your task is to compute the minimum number of posts that must be traversed (including both the starting and the ending posts) when communicating from a given starting post to an ending post. You may choose to go either clockwise or counterclockwise.

After converting the indices to 0-based, one method to compute the result is as follows:

[ \text{clockwise} = (\text{end} - \text{start} + 1) \quad \text{(if } \text{start} \le \text{end}\text{)} ] [ \text{counterclockwise} = (\text{start} + (n - \text{end}) + 1) ]

The final answer is the minimum of these two values. If the starting post index is greater than the ending post index, you may swap them to simplify the calculation.

inputFormat

The input is given via stdin and consists of three lines:

  1. The first line contains an integer \( n \) representing the number of posts.
  2. The second line contains \( n \) space-separated integers representing the angles (in degrees) of the posts.
  3. The third line contains two space-separated integers: the starting post index and the ending post index (1-indexed).

outputFormat

Output a single integer to stdout representing the minimum number of posts that must be traversed (inclusive of the start and end posts).

## sample
6
10 50 90 130 170 310
1 4
4