跳到正文

题解

CF441D Valera and Swaps 题解

Moya_Rao约 9 分钟
原文

这是 2025年11月6日 存档的第 1 版,不是当前版本。查看当前版本

文章信息与版本

文章信息

发布
2025年10月18日
收录
2025年12月12日
更新
2026年11月12日
篇幅
约 9 分钟
题目
CF441D

版本

  1. v2当前
  2. v1
提示 / Tips

这篇题解拥有英文版本。
This article has an English version.

啥?题解区只有一篇题解?这可不行,我也来贡献一篇!

首先题目定义了函数 f(a)f(a),表示最少需要多少次交换操作才能让长度为 nn 的排列 aa 变得从小到大。然后给定一个长度为 nn 的排列 pp,问要执行多少次交换操作才能让 f(p)=mf(p) = m,还要输出字典序最小的交换操作方案。即,如果交换的是 pip_i 和 pjp_j,那么在保证 ii 最小的情况下还要保证 jj 最小。

首先考虑这个 ff 到底该咋算。

如果把按顺序来的排列数组和 pp 数组的关系看作是一个连边关系的情况,也就是说说 ii 连边到 pip_i,连成一个有向图,由于 pp 是个排列,因此这个有向图一定是由一堆环组成的。

假设这里面有 kk 个环,显而易见的,答案就是 n−kn-k。因为一个环并不需要每条边都去操作,可以把它破成链,少去一次操作机会;那么有 kk 个环,次数就是 n−kn-k 啦。

那么我们就可以 O(n)O(n) 地求出一个排列所对应的 ff 值了。

接着要来考虑具体的信息了。看看最初的 f(p)f(p) 与 mm 的关系。

如果 f(p)<mf(p) < m,那么就是要让 f(p)f(p) 增多,怎么增多?当然是让环的个数减少。反过来,如果 f(p)>mf(p) > m,那么就是要让 f(p)f(p) 减少,怎么减少?那就是让环的个数增多啦!

于是现在就只需要考虑如何让 f(p)f(p) 增多或减少了。其实很简单的,如果要增多,就随便选一个由不止一个元素组成的环,然后从里面挑两个数 ii 和 jj,然后交换一下 pip_i 和 pjp_j 即可,这样,这个环就被劈半了。那么要减少怎么办?也很简单,随便选两个不在同一环内的数 ii 和 jj,然后交换一下 pip_i 和 pjp_j 即可,这样,这两个环就合并在一起啦!

一次操作只能增多或减少一个环,因此最少的次数就是 ∣f(p)−m∣|f(p)-m|。

最后想想看,如何让字典序最小?增多的情况,就是先考虑最小的 ii,然后当 ii 所处的这个环中有不止 ii 一个元素的时候,就再从这个环里边找一个最小的 jj,连一下即可;减少的情况,可以直接考虑让 i=1i=1,然后去找最小的 jj,让它不和 11 待一个环里就行,连边即可。

每次操作完成后都要重新算一次 f(p)f(p),因为 pp 改变了,f(p)f(p) 改变了,每个数对应的环也改变了。

这样的时间复杂度是 O(n2)O(n^2) 的,nn 只有 30003000,稳过。

具体求 ff 那里,我记录了 belibel_i 表示 ii 所属的环的编号,每次枚举到一个 belbel 为 00 的 ii,就新开一个编号弄给 belibel_i,并从 ii 出发沿着 pip_i 一直跳环把编号全部给定即可。

提交记录,内含代码。


What? There’s only one solution in the solution section? That’s not enough — I’ll contribute one too!

The problem begins by defining the function f(a)f(a), which indicates the minimum number of swaps required to sort an nn-length permutation aa in ascending order. Given a permutation pp of length nn, the task is to determine how many swaps are needed to satisfy f(p)=mf(p) = m, while producing the swap sequence with the lexicographically smallest order. Specifically, when swapping elements pip_i and pjp_j, both ii and jj must be minimized to ensure the sequence remains lexicographically smallest.

The first thing to consider is how to calculate this ff.

If we regard the relationship between the permutation array and the pp array as a connected edge relationship, that is, ii is connected to pip_i, forming a directed graph. Since pp is a permutation, this directed graph must consist of a bunch of rings.

Suppose there are kk cycles in the graph, the answer is obviously n−kn-k. Since a cycle doesn’t require every edge to be processed, it can be broken into a chain, reducing the number of operations. With kk cycles, the total number of operations is n−kn-k.

So we can then compute the ff value corresponding to a permutation in O(n)O(n) time.

Next, we need to consider the specific information. Let’s examine the relationship between the original f(p)f(p) and mm.

If f(p)<mf(p) <m, we need to increase f(p)f(p) — how? By reducing the number of rings. Conversely, if f(p)>mf(p)> m, we need to decrease f(p)f(p) — how? By increasing the number of rings!

So now we only need to consider how to increase or decrease f(p)f(p). It’s actually quite simple: To increase it, just pick any ring with more than one element, select two numbers ii and jj from it, and swap pip_i and pjp_j – this splits the ring in half. For decreasing, it’s equally straightforward: pick two numbers ii and jj that are not in the same ring, swap pip_i and pjp_j, and these two rings will merge together!

Since each operation can only add or remove one ring, the minimum number of operations required is ∣f(p)−m∣|f(p)-m|.

Finally, consider how to minimize the dictionary order. For the case of increasing elements, start by selecting the smallest ii. If the ring containing ii has more than ii elements, then find the next smallest jj within that ring and connect them. For decreasing elements, directly set i=1i=1 and look for the smallest jj that doesn’t form a ring with 11, then connect them.

After each operation, you have to recalculate f(p)f(p) because pp has changed, f(p)f(p) has changed, and the ring corresponding to each number has also changed.

The time complexity is O(n2)O(n^2), and nn is only 30003000, so it’s stable.

To determine ff, I assign a unique number to each belibel_i representing the ring it belongs to. Whenever I encounter an ii with beli=0bel_i = 0, I assign it a new number. Then, starting from ii, I traverse the ring along pip_i to assign all numbers.

Submit the record containing the code.