File size: 4,125 Bytes
ab396f0 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
Oh boy, Sid's family has taken him to Pizza Planet today! Pizza Planet is a
fun family restaurant with lots of arcade games, but the highlight for Sid is
Space Crane, a crane game with cool toy prizes. He'd love to win some to add
to his collection!
Looking at Space Crane from the front, it can be represented as a 2D plane. At
the top, there's a horizontal crane track, a line segment running from (0,
**M**) to (1,000,000, **M**). There's a claw attached to this track by an
extendible wire. The claw is initially located at coordinates (0, **M**) and
may be moved anywhere within the inclusive range of x-coordinates [0,
1,000,000] and the inclusive range of y-coordinates [0, **M**]. At all points
in time, the connecting wire runs vertically upwards from the claw's position
to the track. That is, when the claw is at some point (**x**, **y**), the wire
forms a line segment from (**x**, **y**) to (**x**, **M**).
Space Crane works differently than most crane games — rather than using the
claw to directly pick up prizes, the player's objective is to navigate the
claw to a series of targets. There are **N** targets, all with distinct
x-coordinates, with the _i_th target at coordinates (**Xi**, **Yi**). If Sid
manages to move the claw to touch the **N** targets in order from 1 to **N**,
and then return the claw to its original position at (0, **M**), he'll win a
prize! Targets are not collected along the way (they're only touched by the
claw), meaning that all **N** targets will remain in place for the duration of
the game.
The claw may never be anywhere directly underneath a target (at the same
x-coordinate but with a strictly smaller y-coordinate), as it would interfere
with the crane's wire. However, the claw may occupy exactly the same position
as a target, including passing directly through targets which Sid is not
currently trying to touch.
Before the game starts, Sid is given an opportunity to adjust each of the
**N** targets. There are two possible choices for each target: it may either
be left in its original position, or its y-coordinate may be increased by
exactly 1 unit. These adjustments may only be performed in advance, and the
targets must then all remain in their chosen positions for the duration of the
game.
Completing the game normally isn't much of a challenge for Sid, but he's heard
a rumour that Space Crane awards double prizes if completed as efficiently as
possible! The game measures efficiency based on how much the claw's wire
expands and contracts. As such, Sid would like to adjust the targets and then
move the claw around such that the total amount of vertical movement (changes
in y-coordinate) performed by the claw is minimized. Note that the claw's
horizontal movement (changes in x-coordinate) is ignored. Help Sid determine
the minimum total amount of vertical claw movement which might be required!
### Input
Input begins with an integer **T**, the number of times Sid plays Space Crane.
For each game, there is first a line containing the space-separated integers
**N** and **M**. Then **N** lines follow, the _i_th of which contains the
space-separated integers **Xi** and **Yi**.
### Output
For the _i_th game, output a line containing "Case #_i_: " followed by the the
minimum total amount of vertical claw movement, in units.
### Constraints
1 ≤ **T** ≤ 100
1 ≤ **N** ≤ 1,000,000
3 ≤ **M** ≤ 1,000,000
0 ≤ **Xi** ≤ 1,000,000
1 ≤ **Yi** ≤ **M** \- 2
### Explanation of Sample
In the first case, the single target's height should be increased from 1 to 2.
Then, aside from moving right and left by 1,000,000 units, the crane will need
to move downwards by 8 units to reach the target and upwards by 8 units to
return to its original position, for a total of 16 units of vertical movement.
In the second case, if the targets are all left at their original heights, 10
units of vertical movement will be required. If their heights are all
increased by 1, 8 units will be required. However, if just the first two
targets are raised, then only 6 units will be required, which is the minimum
achievable amount.
|