L∞ Jumps
Time limit3sMemory limit256 MB
Make exactly n jumps of L-infinity length d from (0,0) to (s,t), minimizing total cost where each jump's direction is ranked from a given offset.
- Level
Hard8 of 10
- Topics
- Divide and conquer, Geometry, Math, Hash map
- Solved
- No attempts yet
Problem
For two points and in the XY-plane, the L∞ distance between them is defined as .
You are given four integers . You start at point and need to move to point . To do so, you perform exactly jumps. Each jump must move exactly in L∞ distance, and the point you reach by a jump must be a lattice point. That is, when you are standing at point , you can move to point with a single jump if and are integers and .
You cannot stop jumping even if you reach the destination before performing all jumps.
Each jump has a cost. You are given more integers with for every . The cost of the -th jump (1-indexed) is defined as follows. Let be the point where you stand just before the -th jump. The lattice points you can jump to are exactly the lattice points on the boundary of a certain square. Assign the integer 1 to point . Then assign to the remaining points of the set in counterclockwise order. Here the positive x direction is to the right and the positive y direction is up. The assigned integer is the cost of jumping to that point.
For example, let , the current position be , and . The reachable points are the 16 lattice points on the boundary of the square , . Point has cost 1, and going counterclockwise, has cost 2, has cost 3, has cost 4, has cost 5, has cost 8, has cost 12, and has cost 16.
Find the minimum total cost to reach the destination.
Input
The input consists of a single test case.
n d s t
x1 y1
x2 y2
...
xn yn
The first line contains four integers. () is the number of jumps. () is the L∞ distance that each jump must cover. and () are the x and y coordinates of the destination. At least one way to reach the destination with jumps is guaranteed to exist.
The -th of the following lines contains two integers and with .
Output
Print the minimum cost required to reach the destination.