公式

B - Slime Swap 解説 by evima


Since slimes of different colors can be freely swapped, being able to arrange them in ascending order through operations is equivalent to having slimes of the same color arranged in ascending order within each color.

Therefore, it suffices to solve the case where all slimes have the same color. In this case, the minimum number of slimes that need color changes equals the total length minus the length of a longest increasing subsequence, so by finding an LIS for each color, we can solve this problem in \(O(N\log N)\).

投稿日時:
最終更新: