#P3842. [TJOI2007] 线段
[TJOI2007] 线段
Description
On an grid, there is a line segment in each row. In row , the segment’s left endpoint is and its right endpoint is .
You start from , must traverse all the segments along the way, and finally reach , with the total distance traveled as short as possible.
More specifically, at any time you may only move one step down (row index increases by ), one step left (column index decreases by ), or one step right (column index increases by ). Since you cannot move up, when moving down from any row to the next, you must ensure you have already completely traversed the segment in the current row.
Input Format
The first line contains an integer .
Each of the next lines contains two integers. In the -th of these lines (the overall -th line), the integers denote and .
Output Format
Output a single integer: the length of the shortest route.
6
2 6
3 4
1 3
1 2
3 6
4 5
24
Hint
One chosen route is:
(1, 1) (1, 6)
(2, 6) (2, 3)
(3, 3) (3, 1)
(4, 1) (4, 2)
(5, 2) (5, 6)
(6, 6) (6, 4) (6, 6)
It is not hard to compute that the total length is .
Constraints: For of the testdata, , .
Translated by ChatGPT 5
京公网安备 11011102002149号