Official

B - Movies Editorial by evima


This is a problem about matching between intervals and points. First, as a solution for a given set of intervals, we consider the following:

  • Sort the intervals in descending order of their left endpoints. For each interval \([l,r]\), if there are unmatched points within \([l,r]\), match it with the largest such point.

We perform DP based on this greedy approach. Sort all intervals in descending order of \(L_i\). Define \(dp[k][l][r]\) as follows:

  • The number of subsets of all intervals \(i\) satisfying \(i \leq k\) and \(l-1\leq R_i \leq r\), such that “when performing the above greedy algorithm, all points \(l,\cdots,r\) are matched, but point \(l-1\) is not matched.”

Consider the transition from \(dp[k]\) to \(dp[k+1]\). The case where interval \(k+1\) is not used is simple: \(dp[k+1][l][r]+=dp[k][l][r]\). Consider the case where interval \(k+1\) is used. Obviously, only \(dp[k][l][r]\) satisfying \(l-1 \leq R_{k+1} \leq r\) are affected.

  • When \(l \leq L_{k+1}\), we should set \(dp[k+1][l][r]+=dp[k][l][r]\).
  • When \(L_{k+1}<l\), point \(l-1\) matches with interval \(k+1\). Here, we need to choose the largest unmatched point less than \(l-1\). If we call this point \(c-1\), the transition is \(dp[k+1][c][r]+=dp[k][c][l-1] \times dp[k][l][r]\).

Implementing the above gives a solution with time complexity \(O(N^5)\). The constant factor is small, so this is sufficient to pass.

Writer’s solution (C++)

posted:
last update: