#P3076. [USACO13FEB] Taxi G
[USACO13FEB] Taxi G
Description
Bessie runs a taxi service for the other cows along a fence of length (). There are cows (). Cow starts at position and wants to go to position (). Bessie starts at the leftmost point, position , must finish at the rightmost point, position , and wants to minimize the total distance she drives.
Bessie’s car can carry only one cow at a time. Cows can enter and exit the car instantaneously. To save gas, Bessie may drop a cow off at an intermediate position before reaching that cow’s destination.
Determine the minimum total distance Bessie must drive to complete all rides and end at position .
Input Format
- Line 1: and separated by a space.
- Lines : The ‑th line contains two space‑separated integers, and (), indicating the starting position and destination position of the ‑th cow.
Output Format
- Line 1: A single integer indicating the total amount of driving Bessie must do. Note that the result may not fit into a 32‑bit integer.
2 10
0 9
6 5
12
Hint
There are two cows waiting along a fence of length . The first cow wants to go from position (where Bessie starts) to position . The second cow wishes to go from position to position .
Bessie picks up the first cow at position and drives to position . There she drops off the first cow, delivers the second cow to her destination, and returns to pick up the first cow. She then drops off the first cow and drives the rest of the way to position .
Translated by ChatGPT 5
京公网安备 11011102002149号