E - Drop Min 解説 by evima
Build a Cartesian Tree and perform tree DP. Consider the number of permutations \(dp[i]\) in the subtree rooted at vertex \(i\). For each vertex, we consider cases based on whether ancestors exist at positions to the left or right of itself.
Thinking about the operations in reverse, vertex \(i\) can be chosen when there exists a larger value adjacent to either side.
When ancestors exist on both left and right of vertex \(i\)
Let \(dp[i]\) be the number of permutations in the subtree rooted at vertex \(i\), assuming there are sufficiently large values at both ends.
Until vertex \(i\) is chosen, we can only choose from either the left descendants or the right descendants, but not both. This is because when choosing vertex \(i\), there must be a larger value adjacent to either side.
Conversely, all other ways to make choices are possible. Before choosing vertex \(i\), that is, when choosing from only one of the left or right descendants, we can freely arrange them within those descendants. After choosing vertex \(i\), the left and right descendants do not interfere with each other.
The number of such ways to make choices (the coefficient for finding it) is \(\binom {L+R}L + \binom{L+R}{L-1} + \binom {L+R}{L+1}\), where \(L\) and \(R\) are the numbers of left and right descendants, respectively. These correspond to the cases where \(i\) is first, \(L\) is first, and \(R\) is first, respectively. \(dp[i]\) is obtained by multiplying this coefficient by \(dp[l]\) and \(dp[r]\).
When ancestors exist on only one side (left or right) of vertex \(i\)
Consider the case where ancestors exist only on the left. (The case for the right is similar.)
Let \(dp[i]\) be the number of permutations in the subtree rooted at vertex \(i\), assuming there is a sufficiently large value at the left end.
Until vertex \(i\) is chosen, we can only choose from the right descendants. This is because when choosing vertex \(i\), there must be a larger value on the left.
Conversely, all other ways to make choices are possible. Before choosing vertex \(i\), when choosing only from the right descendants, we can freely arrange them within those descendants. After choosing vertex \(i\), the left and right descendants do not interfere with each other.
The coefficient is \(\binom {L+R+1}{L+1}\), and we multiply this by \(dp[l]\) and \(dp[r]\).
When ancestors exist on neither left nor right, that is, for vertex \(N\), considering that \(N\) must be chosen first, the coefficient is \(\binom{L+R}{L}\).
The overall computation can be done in \(O(N)\).
投稿日時:
最終更新: