Editorial for WC '17 Contest 3 S2 - GleamingProudChickenFunRun


Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.

This problem can be solved with a greedy approach, by generally considering the clips from earliest to latest (though note that this is not a well-defined ordering itself, as clips have two potential endpoints to sort by).

Let clip i be the clip which ends the earliest (the one with the smallest B value). Assuming that we'd like to exclude clip i from S in an effort to minimize |S|, we'll need to include in S some clip j which overlaps with clip i (such that A_j < B_i). If there are multiple valid clips like this, we should choose the one which ends the latest (the one with the largest B value), so that it's more likely to take care of overlapping with more later-occurring clips. Note that the chosen clip j may end up being equal to clip i. Having included clip j in S, we can effectively ignore all other clips overlapping with it (clips k such that A_k < B_j). This leaves us with just the set of clips k such that A_k \ge B_j, on which the above process can be repeated – that is, we'll next look for the clip which ends the earliest of the remaining ones. Once there are no remaining clips, we can stop, as all clips will have been accounted for (by either having been included in S, or by being known to overlap with a clip in S).

If we sort the clips in non-decreasing order of B (in \mathcal O(N \log N) time), the above greedy process can be implemented naturally in \mathcal O(N^2) time. We can maintain an index i of the next clip which we'll try to omit from S, scan through all N clips each time to find the optimal clip j to include in S, and then move i forwards to the next earliest clip which doesn't overlap with clip j.

Improving the time complexity to \mathcal O(N \log N) is required to earn full marks, and there are several different approaches which accomplish this. One possibility is to create a reduced list of clips in which all clips which are contained entirely within other clips have been omitted. This new list has two useful properties: both the A values and the B values of its clips are sorted, and any optimal clip j which we might want to include in S must still be part of it. We can then maintain an index j into this new list, and move it forwards as necessary when searching for each clip j to include in S, without needing to scan through the entire list each time.


Comments

There are no comments at the moment.