Find the most disjointed segments can select how many

Question one:

N line segments to obtain the most number of line segments do not intersect.

Ideas:

This is a classic problem. The specific approach is greedy, you can also use dp.

Greedy approach: press the right sort endpoints can choose to choose, can not not choose. Because if you can not choose, it will result in the addition of at least one segment of the exit, and also makes the rightmost point of the current segment set to the right, and certainly not excellent.

dp practice: f [i] represents the coordinates to i Select up to how many, to enumerate segment i is transferred to the starting point may need to be discrete.

 

Question two:

To n line segments, segments can pan, only need to meet the right end point of not more than one value can, of course, the left end point can not be less than 0, up to ask how many segments are mutually exclusive.

Ideas:

This question we really debated for a long time. Finally, the nlog of a greedy approach. And I think it is a very bloated n ^ dp 2 space-time, but the original title data 1e4, it should also be legal.

First get the obvious corollary: there must be no gap between the segments, because if there is a certain gap may be left without the right to the more inferior. If you press the right endpoint is incremented to sort them, then the final program segment number must be incremental, because if not increasing, exchange two segments will not reverse the more inferior.

Greedy approach: press the right sort endpoint, the current set of selected segments of maintaining maximum length of a return stack, in turn enumerate each line segment, if applied directly to the end of the current line can be set, then added to the list, if you do not can compare it to the length of the top of the heap segment, leaving a relatively short length, sweep again the answer came out. It uses two conclusions above.

This is a "can go back on the greedy", it is worth learning.

dp practice: RIGHT sorting endpoint, f [i] [j] denotes the time coordinate i, a method selected from 3 up to how many number of the last line segment j, consider violence n ^, j is enumerated for each of the back All the lines are transferred, consider optimization, we find proceeds to f [j] [k] of the case, in fact, it is f [i] [1 ~ k-1] of the transfer, so we only need to maintain a maximum prefix We can achieve the transfer of O1. Note that this method can not be discrete, that is, if the coordinates of a large range can not be used, because we can not find the length of all segments of lengths can be combined and discrete.

This optimization by way of a one-dimensional dp prefix record maximum method should be of universal significance, need to know.

Guess you like

Origin www.cnblogs.com/hyghb/p/12405236.html