CF1753C Wish I Knew How to Sort
Alex_Wei
·
·
题解
Wish I knew how I can come up with the solution.
这题把我送走了。
注意到最终态形如前面 cnt_0 个位置(称为前缀)上是 0,后面 cnt_1 个位置(称为后缀)上是 1。
改变前缀上 0 的个数的交换,必然形如选择前缀上的 1 和后缀上的 0 交换。设当前前缀上 1 的个数为 x,则后缀上 0 的个数必然为 x,共有 x ^ 2 对选择后可以将 x 减去 1 的 (i, j)。因此,每步操作令 x 减 1 的概率为 \dfrac {x ^ 2} {\binom n 2}。
综上,设初始前缀上 1 的个数为 x,则答案为 \dbinom n 2 \sum\limits_{i = 1} ^ x \dfrac 1 {x ^ 2}。时间复杂度 \mathcal{O}(n\log n) 或 \mathcal{O}(n)。代码。