Feeding Frenzy
Farmer Tom has recently acquired some sheep for his farm, and he needs to figure out how to feed them efficiently. These sheep have highly rigid schedules — they must each eat at a specific time in the day each day, or they will not eat.
Each sheep requires one bucket of food to be ready at precisely the time which they are scheduled to eat. However, after each sheep eats, a certain amount of time is required for the farm hands to clean the bucket before it can be used to feed another sheep. Tom wants to minimise the buckets he needs to purchase, but it is also vital that there is a bucket ready to feed the sheep at the exact time that they need feeding each day.
How many buckets will Tom need at minimum to feed all of the sheep at their scheduled time?
Input
The first line contains two space-separated integers and
— the number of sheep and the amount of time required to clean each bucket immediately after its use, respectively.
The next line contains space-separated integers
, the time of day at which the
th sheep needs to be fed, given in non-decreasing order.
Output
Output the minimum number of buckets required to feed all sheep, where each sheep needs to be fed at a certain time and each bucket needs time to pass after being used before it can be reused.
Example
Input 1
5 3
1 2 3 4 10
Output 1
3
- The sheep at time
obviously requires a new bucket. The same bucket will be available again at time
.
- The sheep at time
requires a new bucket. The bucket will become available at time
.
- The sheep at time
requires a new bucket.
- The sheep at time
can reuse the previous bucket from the sheep at time
, as it has been cleaned.
- By time
, all buckets will have been cleaned, so we can use any of them.
Input 2
4 2
5 5 5 5
Output 2
4
Each sheep requires its own bucket since they all must be fed at the same time, so there is no way to clean them in between feeding schedules.
Input 3
6 1
1 2 2 3 4 5
Output 3
2
Since each bucket only needs second to be cleaned, the only case where multiple buckets are needed is if multiple sheep need to be fed at the same time. Since
sheep need to be fed at time
,
buckets are required.
Comments