#P8623. [蓝桥杯 2015 省 B] 移动距离

[蓝桥杯 2015 省 B] 移动距离

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,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 66, 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 mm and nn, find the shortest moving distance between them. (Diagonal movement is not allowed.)

Input Format

The input contains 33 integers w,m,nw, m, n separated by spaces, all in the range from 11 to 1000010000.

Here, ww is the row width, and m,nm, n are the building numbers to compute.

Output Format

Output one integer, representing the shortest moving distance between buildings mm and nn.

6 8 2
4
4 7 20
5

Hint

Time limit: 1 second, 256M.

Lanqiao Cup 2015 Provincial Contest Group B, Problem H.

Translated by ChatGPT 5