#P1180. Fuel and Food Expense Optimization
Fuel and Food Expense Optimization
Fuel and Food Expense Optimization
You are given a car with a fuel tank of capacity c liters and a mileage of m kilometers per liter. The total distance from the starting city to the destination is d kilometers. Along the route there are n gas stations. Each gas station is described by its distance from the starting city and its fuel price (in yuan per liter). At the start, the tank is full.
The driver follows these habits:
- If the fuel remaining in the tank is at least half of its capacity (c/2) and is sufficient to reach the next station (or the destination), the driver will never stop at that station.
- The first time the driver is forced to stop (i.e. when the fuel is insufficient or below half‐tank), he always fills the tank to full.
- Every time the driver stops at a gas station (regardless of refueling amount), he spends an extra 20 yuan for food (e.g. buying fast food).
- At every gas station, the payment for fuel is computed as the product of the fuel purchased and the station’s price per liter, and the amount is rounded to one decimal place (using standard rounding: round half up).
- The driver knows the car’s mileage, so he can compute the fuel required to travel between two points as distance/m.
When not forced to stop, the driver will simply pass the station. However, if at a station the fuel remaining is not enough to cover the next leg or is below c/2, he must stop. If it is the first stop then he fills up to full capacity. For any subsequent forced stop, to minimize cost (since each stop incurs an extra food cost of 20 yuan), the driver buys only the minimal amount of fuel needed so that after traveling the next leg he would ideally have exactly half a tank remaining. In formulas, if at a station the current fuel is f and the next leg requires R liters (i.e. R = (next_distance)/m), the driver will try to purchase an amount x such that
\( f + x - R = \frac{c}{2} \)
If the computed f+x would exceed the tank capacity, then he simply fills up to full.
Your task is to compute the minimum total expense (the sum of fuel costs and food costs paid at stops) needed for the journey. It is guaranteed that the journey is feasible.
inputFormat
The input consists of several lines. The first line contains four numbers: c m d n, where
- c (a positive number) is the fuel tank capacity (in liters).
- m (a positive number) is the mileage, in kilometers per liter.
- d (a positive number) is the total distance from the starting city to the destination (in kilometers).
- n (a nonnegative integer) is the number of gas stations along the route.
Each of the next n lines contains two numbers: distance and price, where distance (in kilometers) is the position of a gas station from the start, and price is its fuel price (in yuan per liter). The gas stations are given in increasing order of distance.
outputFormat
Output a single number representing the minimum total expense (in yuan) for the journey. The answer should be calculated using the rules described and the fuel cost at each station is rounded to one decimal place before adding the additional 20 yuan food cost.
sample
40 10 500 1
200 3.25
85.0