Cow Cars
Time limit1sMemory limit128 MB
Assign cows to M lanes so each cow's speed minus D times the number of cows ahead in its lane stays at least L, maximizing the number of cows used.
- Level
Medium6 of 10
- Topics
- Greedy, Sorting, Binary search, Implementation
- Solved
- No attempts yet
Problem
There are () cows, numbered through , each driving a separate car along a highway. The highway has () lanes, and every cow may drive in any lane. Cow has a maximum speed of () km/h.
To avoid collisions, cow slows down by () km/h for each cow ahead of it in the same lane. So if cows are ahead of cow in its lane, that cow travels at km/h. The cows are spaced far enough apart that no crashes occur once they slow down this way.
A minimum-speed law requires every cow on the highway to travel at a speed of at least () km/h, so some cows may be unable to use the highway. Find the maximum number of cows that can drive on the highway while obeying the minimum-speed law.
Input
- Line 1: four space-separated integers , , , and .
- Lines to : line contains a single integer , the maximum speed of cow .
Output
- A single integer: the maximum number of cows that can use the highway.
Hint
With three cows, one lane, a per-cow slowdown of , and a minimum speed of , at most two cows can drive. Put a cow of speed first (it travels at ) and the cow of speed second (it travels at ); both meet the minimum speed of .