公式

C - Partially Sort 解説 by evima 印刷ページ


This problem asks us to find, over all subsequences of \(P\), the sum of the number of inversions after sorting only the elements at the chosen subsequence positions. The inversions can be classified into three types of contributions: pairs where neither element is chosen in the subsequence, pairs where exactly one element is chosen, and pairs where both elements are chosen.

The contribution of the first type is the number of originally inverted pairs \(I\) multiplied by the number of ways of choosing whether to include each of the other \(N-2\) elements in the subsequence, which is \(2^{N-2}I\). The contribution of the third type is \(0\), since sorted subsequence elements cannot form inverted pairs.

The issue is the second type. Suppose \(P_i\) is not chosen; consider the number of inversions between \(P_i\) and the other elements chosen in the subsequence. First, classify the values of \(P\) into the following four types.

  • \(j\) satisfying \(j < i\) and \(P_j < P_i\)
  • \(j\) satisfying \(j < i\) and \(P_j > P_i\)
  • \(j\) satisfying \(i < j\) and \(P_i < P_j\)
  • \(j\) satisfying \(i < j\) and \(P_i > P_j\)

Suppose there are \(A,B,C,D\) of the above, respectively, and that \(a,b,c,d\) of them, respectively, are chosen in the subsequence. Then, the number of inversions between \(P_i\) and the elements chosen in the subsequence is \(B+D-2\min(b,d)\). That is, we need to compute \(\sum_{b=0}^{B} \sum_{d=0}^{D} \binom{B}{b} \binom{D}{d} \min(b,d)\).

Since \(\min\) is hard to handle, add the condition \(b \le d\). Then we need to compute \(\sum_{b=0}^{B} \sum_{d=b}^{D} \binom{B}{b} \binom{D}{d} b\). This expression can be interpreted as follows.

  • There are \(B\) mutually distinguishable red balls and \(D\) mutually distinguishable blue balls. We choose some of these balls so that the number of chosen red balls is at most the number of chosen blue balls. Find the sum, over all such ways of choosing, of the number of chosen red balls.

Considering the contribution per red ball, this reduces to the following problem.

  • There are \(B-1\) mutually distinguishable red balls and \(D\) mutually distinguishable blue balls. We choose some of these balls so that the number of chosen red balls is at most the number of chosen blue balls minus \(1\). Find the number of such ways of choosing.

By flipping whether each blue ball is chosen, there is a one-to-one correspondence between “ways of choosing such that the number of chosen red balls is at most the number of chosen blue balls minus \(1\)” and “ways of choosing at most \(D-1\) balls in total from the red and blue balls.” That is, we need to compute \(\sum_{i=0}^{D-1} \binom{B+D-1}{i}\).

The case \(b > d\) can be handled similarly. Thus, it suffices to compute an appropriate prefix sum \(2N\) times; this is a well-known problem that can be solved in \(\mathrm{O}(N \sqrt N)\) using, for example, Mo’s algorithm. Hence, this problem can be solved in \(\mathrm{O}(N \sqrt N)\).

投稿日時:
最終更新: