Given N intervals, find the fewest half-integer cut points so that every interval contains at least one chosen point.
Medium6GreedyIntervalsSortingInterviewNo attempts yetTime limit2sMemory limit512 MBFarmer John keeps N cows on leashes. Every leash is tied to a stake driven in at an integer position beside a straight fence that runs east to west and is at most 5,300,000 meters long. Each cow pulls her leash as far east as it goes, but never past the end of the fence. A leash whose stake sits at position s and whose length is l covers the stretch from s to s+l.
Farmer John's wife thinks leashes are barbaric, so she came to the fence with her dull butcher knife. The knife is good for only a few cuts, so she wants to make as few as she can. For one cut she stands halfway between two neighboring integer positions, at x+0.5, and severs every leash that passes in front of her. The leash with stake s and length l is severed at x+0.5 when s≤x and x+1≤s+l.
You are given the stake position and the length of every leash. Find the smallest number of cuts that frees all of the cows.
The first line contains the number of cows N (1≤N≤32000).
Each of the next N lines contains two space separated integers that describe one leash. The first is the position of its stake and the second is the length of the leash. Both values are positive, and the position plus the length is at most 5,300,000.
Print, on one line, the minimum number of cuts needed to cut every leash at least once.
The picture below shows where seven leashes lie. The top two rows give the integer positions along the fence.
1 1 1 1
1 2 3 4 5 6 7 8 9 0 1 2 3
-------------------------
. 111111111 . . . . . . .
. . . 222222222222222 . .
. . 3333333 . . . . . . .
. . . . 4444444 . . . . .
. . . . . . . . 555555555
66666666666 . . . . . . .
. . . . . . 7777777 . . .
A cut at 5.5 severs leashes 1, 2, 3, 4, and 6. A second cut at 9.5 severs leashes 5 and 7. Other positions work too, but no single cut reaches both leash 5 and leash 6, so at least two cuts are needed.