F - Many Increasing Problems 解説 by evima
First, let’s solve Increasing Problem. Consider the function \(f_i(x)\) defined as the minimum number of operations required to make \(A_1,A_2,\dots,A_i\) non-decreasing and \(A_i = x\). Its transitions are as follows:
- \(f_0(x) = 0\)
- \(f_i(x) = \min_{y \le x} f_{i-1}(y) + |x - A_i|\)
We can solve Increasing Problem in \(\mathrm{O}(N\log N)\) time by managing this function \(f_i(x)\) using the slope trick. However, it’s a bit tricky to handle as is. By considering what happens inside the slope trick, we can see that the answer to Increasing Problem is obtained as follows.
- For a multiset of integers \(X\) that is initially empty, perform the following for \(i=1,2,\dots,N\) in this order.
- Add two copies of \(A_i\) to \(X\).
- Let \(v\) be one of the greatest elements of \(X\). Add \(\max(v-A_i,0)\) to the answer. Remove (just one copy of) \(v\) from \(X\).
Now, let’s solve the following problem for \(1 \le s \le M-1\) and take the sum of the answers:
- For all \(M^N\) sequences \(A\), solve Increasing Problem for the sequence \(B\) obtained by replacing each element of \(A\) less than or equal to \(s\) with \(0\) and each element greater than \(s\) with \(1\), and find the sum of the answers.
It’s easy to verify that this yields the same result as the original problem. From here on, we’ll consider solving the transformed problem for all \(s\). The answer to Increasing Problem for a binary sequence can be rephrased as follows:
- Initially, you are at coordinate \(0\). Perform the following operation in the order \(i=1,2,\dots,N\).
- If \(B_i = 0\), go to (your current coordinate \(-1\)). However, do nothing if you are at coordinate \(0\).
- If \(B_i=1\), go to (your current coordinate \(+1\)).
- After all operations, the answer is the number of times you have moved \(-1\).
Now, suppose you moved \(-1\) in the \(i\)-th move. Let’s say you moved from coordinate \(z\) to \(z-1\) then. It is always possible to take the most recent move from coordinate \(z-1\) to \(z\). Let this be the \(j\)-th move. Then, the \((j+1)\)-th to the \((i-1)\)-th moves must be like the Catalan numbers, repeating \(+1\) and \(-1\) so that the minimum cumulative sum is \(0\) and the total sum is also \(0\). That is, for a fixed \(k:=j-i-1\), you should multiply the following values. (Here, \(k\) is even.)
- The number of candidates for \(i\): \(N - k - 1\)
- The number of ways to perform the \((j+1)\)-th to the \((i-1)\)-th moves: \(C_{\frac{k}{2}} \times {s(M-s)}^{\frac{k}{2}} = \frac{k !}{\frac{k}{2}! \left(\frac{k}{2}+1\right)!}\times {s(M-s)}^{\frac{k}{2}} \)
- The number of candidates for the elements of \(A\) before the \((j-1)\)-th or after the \((i+1)\)-th term: \(M^{N - k - 2}\)
By summing these for all \(k\) as a polynomial of \(s(M-s)\) and evaluating at multiple points \(1(M-1),2(M-2),\dots,(M-1)1\), we can solve the transformed problem for all \(s\). The time complexity is \(\mathrm{O}(M\log^2 M + N \log N)\), which is sufficiently fast. Also, by replacing this with a polynomial of \(s\) and calculating the \(i\)-th power sum using \(\frac{e^{(M+1)x}-1}{e^x-1}\), it is possible to solve it in \(\mathrm{O}(N\log N)\) time.
(Modified the first draft written by GPT-4.)
投稿日時:
最終更新: