P8623 [Lанqiao Cup 2015 NOI Qualifier B] Moving Distance

Description

In the residential area on Planet X, all buildings are identical and are arranged in a grid. The buildings are numbered $1, 2, 3, \cdots$. When one row is filled, the numbering in the next row continues from the adjacent building but in the opposite direction. For example, when the row width is $6$, the numbering starts like this: ``` 1 2 3 4 5 6 12 11 10 9 8 7 13 14 15 ..... ``` The task is: given two building numbers $m$ and $n$, find the shortest moving distance between them. (Diagonal movement is not allowed.)

Input Format

The input contains $3$ integers $w, m, n$ separated by spaces, all in the range from $1$ to $10000$. Here, $w$ is the row width, and $m, n$ are the building numbers to compute.

Output Format

Output one integer, representing the shortest moving distance between buildings $m$ and $n$.

Explanation/Hint

Time limit: 1 second, 256M. Lanqiao Cup 2015 Provincial Contest Group B, Problem H. Translated by ChatGPT 5