DMOPC '14 Contest 6 P4 - Bus Jam

View as PDF

Submit solution

Points: 10 (partial)
Time limit: 1.0s
Memory limit: 128M

Problem type

On a certain route, buses are initially evenly spaced; however, due to the inconsistent traffic flow, one bus can get slightly delayed. As that bus becomes increasingly crowded due to the greater number of passengers at each stop, it gets delayed even further while the bus behind it catches up. This leads to buses travelling in packs where the first bus is full and the last one is almost empty.

As the buses (and especially the passengers) get jammed, one solution is to make the almost empty buses wait for a certain number of minutes so that there can be a reasonable gap.

Given a list of buses and the length of time that it is away from the terminus, find the minimum number of M-minute breaks required so that all buses will have headways (the length of time between buses) of at most H minutes.

Each bus has the choice of taking any number of M-minute breaks, even if it means being overtaken by another bus or waiting until the next day.

Input Specification

The first line of input will contain three space-separated integers, N (1 \le N \le 1\,000), which represents the number of buses, M (1 \le M \le 100), the duration of one break, and H (1 \le H \le 100), the desired headway (length of time between buses).

The next N lines contain T (0 \le T \le 1\,000), the length of time in minutes in which the bus is expected to arrive at the terminus. The times will be sorted in ascending order.

Output Specification

Output the minimum number of M-minute breaks that will occur.
It is guaranteed that it will be possible to achieve a headway of at most H minutes.

Sample Input

5 2 10

Sample Output


Explanation for Sample Output

The first bus can take two breaks while the second and third buses can take one break each.
All headways are now 10 minutes or less, as required.

bus pack


  • 3
    d  commented on Dec. 26, 2015, 6:38 p.m. edited

    What would be the output for this case?

    3 9 5

    • -1
      sz_8  commented on Sept. 22, 2022, 9:59 p.m. edited

      Output from my solution that AC'd:


  • 0
    pyrexshorts  commented on April 7, 2015, 5:08 p.m. edited

    Between the first bus and the second bus, there is 12 minutes of headway, but there needs to be 2 breaks? Shouldn't it be 1 break so that the headway is 10 minutes?

    Edit: nvm, got it.

    • 0
      Sentient  commented on April 7, 2015, 5:16 p.m.

      If the first bus takes a break, its value changes to 3, but then the other buses also have to take breaks...try solving for the sample on paper...