Bus Stops

View as PDF

Submit solution

Points: 10
Time limit: 1.0s
Memory limit: 16M

Problem type

If you take the TTC, you'll know that often buses can only carry a certain number of people (call this C).
Sometimes, you get left stranded at the bus stop when the bus is full.
If you're lazy, you'll just wait for the next bus. But sometimes, it might just be better to walk to another stop beforehand.

In our scenario, there is only one bus, and so some people might be required to walk.
Let's suppose you know in advance all N of the passengers and where they want to get on and off.
Now, you want to create a plan so that each passenger spends the least amount of time in transit to reach his/her destination.
All passengers are treated equally - we just want to minimize the total time for all passengers.

The bus starts at bus stop 1 and goes "rightward", going to bus stop 2, then 3, etc.
The bus stops, numbered 1, 2, \dots, B, are located along a perfectly straight street.
It takes 5 minutes to walk from one bus stop to the next, and 1 minute to do the same by bus.

Input Specification

Line 1: Three integers, passengers N (N \le 1\,000\,000), stops B (B \le 1\,000\,000), capacity C (C \le N).
Next N lines: a pair of integers, representing the start and end stops of a passenger.

Passengers get to their starting stops instantly.
Assume that they get to their required bus stop right on time: they won't have to wait for the bus.
All numbers will be positive integers.

Output Specification

The total time taken for all the passengers to get to work, assuming an optimal schedule.

Sample Input 1

3 5 2
1 5
2 5
3 4

Sample Output 1

12

The bus only holds 2 people, so the third guy has to walk. (No passenger is favoured over another)
Passenger 1: takes the bus (4 minutes)
Passenger 2: takes the bus (3 minutes)
Passenger 3: walks from 3 to 4 (5 minutes)

Sample Input 2

5 8 1
1 3
2 4
2 5
6 7
7 8

Sample Output 2

21

In this case, the bus is more like a taxi.
Passenger 1: Just takes the bus (2 minutes)
Passenger 2: walks to stop 3 first (5 minutes) and then takes the bus to 4 (1 minute)
Passenger 3: walks to stop 4 first (10 minutes) and then takes the bus to 5 (1 minute)
Passengers 4 and 5: just take the bus (1+1 minutes)


Comments

There are no comments at the moment.