Interval related problems (greedy)

Excerpted from Chapter 8 of the Purple Book

The breakthrough is generally how to choose when the interval is included, and how to sort it

(1) Select a disjoint interval

Problem: There are n open intervals (ai, bi), choose as many intervals as possible, so that these intervals have no common point.


Sort by b from small to large (a doesn't matter in this case, the result is the same)


The first interval must be selected, and then all the intervals that intersect with interval 1 are excluded, and then the next one is selected if they do not intersect, and the same is true.


So just record the interval number, scan it once and it will be ok.


(2) interval selection

Problem: There are n closed intervals [ai, bi] on the number line. Take as few points as possible so that each interval has at least one point.


According to b from small to large, b and a from large to small, the last point of the first interval is taken each time, when this point cannot be covered


Just take the last point of the next interval, and so on.


(3) Interval coverage

There are n closed intervals [ai, bi] on the number line, and select as few intervals as possible to cover a specified line segment [s, t].


Preprocessing, the part outside [s, t] is cut off (because it doesn't have any effect)


According to the order of a from small to large, the starting point of interval 1 is not that s has no solution. Otherwise, choose the longest interval with the starting point in s


Then the new starting point is the end point bi of the interval just selected, then ignore the part before bi, and continue to do


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326007256&siteId=291194637
Recommended