There are n lovely children standing in a row, numbered from 1 to n from left to right. Their positions are also numbered from 1 to n from left to right. Initially, no one has shaken hands with others. Their teacher is playing a game. In each turn, the teacher chooses two adjacent children and let them shake hands with each other. After shaking hands, the two children will swap their positions. After m turns, the teacher asks you a question: can you choose some children as many as possible such that every pair of chosen children has shaken hands with each other?
\(1\le n,m\le 2\times10^5\)
如果 \(a\) 和 \(b\) 没有交换过,\(b\) 和 \(c\) 没有交换过,那么 \(a\) 和 \(c\) 也没有交换过。
如果两个数没有交换过就从大的向小的连边,这个图是原图的补图,那么我们现在要求这个图的最大独立集。
这是一个偏序集,最长反链等于最小链覆盖。现在问题变成了有一个二分图,在当中扣掉了 \(m\) 条边。求这个图的最大匹配。
有一种贪心的做法:按照出度从小到大考虑,每次找出度最小的匹配。
现在证明这种做法的答案是 \(O(n\sqrt m)\) 级的。
考虑一条边都没有被扣掉的情况,此时第 \(i\) 个点的度数为 \(i\)。设前 \(i\) 条边匹配了 \(p_i\) 对。那么 \(i\) 没有匹配上当且仅当 \(p_i<in_i\)。如果你要使第 \(i\) 个点每匹配上,你需要把 \(1\) 到 \(i\) 全部减 1。所以最多有 \(\sqrt m\) 个没有匹配上的点。
对于剩下的点,我们可以跑匈牙利来匹配。由于二分图是完全二分图扣掉了一个 \(m\) 条边的二分图,不能直接跑匈牙利。考虑用并查集维护下一个没有到达过的点,如果这个点是被扣掉的边,再看下一个。否则就直接跑,同时这个点标记为到达过。把并查集往上合并。这样子复杂度是 \(O(n\log n)\),总复杂度 \(O(n\sqrt m\log n)\)