公式

C - Not Argmax 解説 by evima


DP is applied here. Let \(dp[l][r]\) denote the answer when considering only the interval \([l,r]\). We assume that when considering the interval \([l,r]\), only the intervals \([L_i,R_i]\) that satisfy \(l \leq L_i \leq R_i \leq r\) are given as conditions. Without loss of generality, we can assume that the values of \(P_l,\cdots,P_r\) range from \(1\) to \(r-l+1\).

Let’s consider how to calculate \(dp[l][r]\). We will do a case-by-case analysis based on the position \(m\) of the maximum value of \(P_l,\cdots,P_r\). If there is a condition with \(X_i=m\), then that \(m\) is invalid. Consider the case without one. First, the conditions that satisfy \(L_i \leq m \leq R_i\) can be ignored from this point onward. This reduces the problem to calculating \(dp[l][m-1]\) and \(dp[m+1][r]\). Thus, we obtain the transition for \(dp\). Note that, since there is flexibility in the allocation of value sets to the left and right sides, it is necessary to multiply by the binomial coefficient corresponding to this.

This gives us an \(O(N^3)\) DP solution. Here, it is necessary to enumerate the prohibited \(m\) for each \([l,r]\), but checking the \(M\) conditions each time would take \(O(N^2 M)\) time, which is too slow. By fixing \(l\) and managing the positions of prohibited \(m\) while increasing \(r\), it takes \(O(NM)\) time in total, which is fast enough.

The overall time complexity is \(O(N^3+NM)\).

Sample Code (C++)

投稿日時:
最終更新: