公式

F - Zonal Score Maximization 解説 by evima 印刷ページ


If \(N\) is even

Dividing \(P\) into subsequences of length \(2\) gives a sum of scores equal to \(\sum_{i=1}^{N} P_i\), which is the maximum possible sum of scores.
Thus, if \(X = \frac{N(N+1)}{2}\), then any permutation \(P\) satisfies \(f(P) = X\), so if \(c\) is the number of \(-1\)s in \(Q\), the answer is \(c!\).
Otherwise, the answer is \(0\).

If \(N\) is odd

If any contiguous subsequence after the division has length \(4\) or more, we can further divide it to increase the sum of scores, so we may assume each contiguous subsequence has length at most \(3\). The number of elements to the right of the leftmost contiguous subsequence of length \(3\) or more is even, and all of them can be included in the score, so we have:
\(f(P) = \frac{N(N+1)}{2} - \min(\text{median of } (P_i, P_{i+1}, P_{i+2}) \mid i = 1, 3, \dots, N-2)\).
Let \(p = \frac{N(N+1)}{2} - X\).
The answer can be obtained by subtracting the number of permutations \(P\) for which \(\min(\text{median of } (P_i, P_{i+1}, P_{i+2}) \mid i = 1, 3, \dots, N-2)\) (denoted \(M\)) is at least \(p+1\) from the number for which it is at least \(p\).
We explain how to count the number of permutations \(P\) for which \(M\) is at least \(p\) (the case of \(p+1\) is analogous).
Let \(B_i = \begin{cases} 0 & (P_i < p) \\ 1 & (p \leq P_i) \end{cases}\).
Then the condition for \(M\) to be at least \(p\) is that \(B_{2i-1} + B_{2i} + B_{2i+1} \geq 2\) for \(i = 1, 2, \dots, \frac{N-1}{2}\).
For \(i\) with \(Q_i = -1\), the value of \(B_i\) is not fixed; otherwise it is fixed. Also, \(B\) as a whole must contain exactly \(p - 1\) zeros.
Let \(g_{l,r,a,b}\) denote the formal power series obtained by summing \(x^{\sum_{i=l+1}^{r} B_i}\) over all ways to assign the \(l\)-th through \(r\)-th entries of \(B\) such that \(B_l = a\) and \(B_r = b\). When \(i_1, i_2, i_3\) are odd, the following holds:
\(\begin{pmatrix} g_{{i_1},{i_3},0,0} & g_{{i_1},{i_3},0,1}\\ g_{{i_1},{i_3},1,0} & g_{{i_1},{i_3},1,1} \end{pmatrix}= \begin{pmatrix} g_{{i_1},{i_2},0,0} & g_{{i_1},{i_2},0,1}\\ g_{{i_1},{i_2},1,0} & g_{{i_1},{i_2},1,1} \end{pmatrix} \begin{pmatrix} g_{{i_2},{i_3},0,0} & g_{{i_2},{i_3},0,1}\\ g_{{i_2},{i_3},1,0} & g_{{i_2},{i_3},1,1} \end{pmatrix} \).
Thus, if we can compute the product of \(\begin{pmatrix} g_{{2i-1},{2i+1},0,0} & g_{{2i-1},{2i+1},0,1}\\ g_{{2i-1},{2i+1},1,0} & g_{{2i-1},{2i+1},1,1} \end{pmatrix}\) over \(i = 1, 2, \dots, \frac{N-1}{2}\) in this order, we can find the number of sequences \(B\) containing exactly \(p - 1\) zeros. This can be computed in \(O(N (\log N)^2)\) using divide-and-conquer and convolution.
As in the case \(N\) is even, multiplying by the appropriate factorials converts the count of sequences \(B\) to the count of permutations \(P\).

投稿日時:
最終更新: