Calculating Taxi Fare
Time limit1sMemory limit128 MB
Given a sequence of streets with lengths and per-kilometer times, compute a passenger's fare between two streets using tiered per-kilometer pricing plus night and traffic surcharges.
- Level
Medium7 of 10
- Topics
- Simulation, Implementation, Math, Prefix sum
- Solved
- No attempts yet
Problem
Taxi fares are complicated: they depend on how far you travel, the time of day, and the traffic. A taxi drives through a sequence of streets in this exact order. Street is kilometers long, and the taxi keeps a constant speed on it, taking minutes to drive one kilometer of .
A passenger boards at the start of some street and gets off at the end of a later street (boarding and alighting always happen at a street's end, never in the middle of a street). The passenger is charged per kilometer:
- Each of the first 10 kilometers costs 1000 Rials.
- Each of the next 20 kilometers (kilometers 11 through 30) costs 250 Rials.
- Each kilometer after the 30th costs 100 Rials.
Two surcharges may then apply:
- Night surcharge. Consider each kilometer separately. If the taxi spends at least one minute driving that kilometer during the interval from 12:00 AM to 6:00 AM, that kilometer costs 20% more.
- Traffic surcharge. If the average speed over the whole trip is less than 30 km/h, the fare is increased by 10%.
The night surcharge is added per kilometer first; the 10% traffic surcharge is then applied once to the resulting total.
Input
The input contains several test cases. Each test case has two parts.
The first part lists the streets the taxi drives through, one per line and in travel order:
street-name length min
Here street-name is a unique string of at most 20 letters and digits with no spaces, length is in kilometers (at most 200), and min is in minutes; both are positive integers. Each street is visited exactly once. This part ends with a line containing only a single $ character.
The second part is one line:
source-street dest-street time
source-street and dest-street are the boarding and alighting street names, and time is the boarding time in 24-hour HH:MM format. Both streets belong to the street list, and the destination street does not come before the source street. A line containing only a single # character ends each test case.
The input ends with a line containing two dash characters --.
Output
For each test case, print a single line containing the fare of the passenger's trip.