Keep on Truckin'
InterviewTime limit1sMemory limit128 MB
Given fixed and added motel distances, count overnight stop sequences where each day covers between A and B km.
- Level
Medium4 of 10
- Topics
- Dynamic programming, Array, Sorting
- Solved
- No attempts yet
Problem
A truck driver plans to drive along the Trans-Canada Highway from Vancouver to St. John's, a distance of km, stopping each night at a motel.
The following motels are always available, listed by their distance in km from the starting point in Vancouver:
0, 990, 1010, 1970, 2030, 2940, 3060, 3930, 4060, 4970, 5030, 5990, 6010, 7000
More motels may be added just before the trip begins.
The driver can complete the journey only if all of the following hold:
- each day the driver travels at least km (a company rule),
- each day the driver travels at most km (a legal limit), and
- every night is spent at an eligible motel (either from the fixed list above or from the additional locations given in the input).
The trip starts at km (Vancouver) and ends at km (St. John's). Count the number of different ways the driver can choose the sequence of overnight motels so that every daily distance is between and , inclusive.
For instance, with no added motels: if and the trip is impossible, so the answer is ; if and there is exactly one plan; and if and there are four plans. If , and one extra motel is added at km , there are two plans.
Input
- The first line contains the minimum daily distance .
- The second line contains the maximum daily distance , with ; both are integers.
- The third line contains an integer (), the number of additional motels.
- Each of the next lines contains one integer (), the distance of an additional motel from Vancouver.
No two motels are located at the same distance from Vancouver.
Output
Print a single integer: the number of different ways the driver can choose the overnight motels to complete the trip under the given constraints.