Seat each arriving researcher at a freed workstation that stayed unlocked to save the most unlocks.
Medium5GreedyHeapSortingInterviewNo attempts yetTime limit10sMemory limit256 MBChansol manages a supercomputer that was installed recently. His job is to assign a workstation to every researcher who comes in to run a computation.
Unlocking a machine for each arriving researcher annoys Chansol, so he ignores the security policy and asks everyone to leave without locking the workstation. As long as an unlocked and empty workstation is around, he can seat the next researcher there and skip the unlock.
An empty workstation locks itself once the idle time passes m minutes. An idle time of exactly m minutes still leaves it unlocked. Researcher i arrives at minute ai, stays for exactly si minutes, and leaves at minute ai+si. One workstation seats one researcher at a time.
If a researcher leaves a workstation at minute t, that workstation passes to any other researcher arriving between minute t and minute t+m, both ends included, with no unlock. Assume there are enough workstations. Find the largest number of unlocks Chansol avoids when he assigns the workstations in the best possible way.
The first line has the number of researchers n and the idle limit m after which a workstation locks itself. (1≤n≤300000, 1≤m≤108)
Each of the next n lines has the arrival minute a and the stay length s of one researcher, separated by a space. (1≤a,s≤108)
Print on one line the maximum number of unlocks Chansol can save.