跳到正文

题解

题解:AT_arc209_d [ARC209D] A_A_i

P2441M约 6 分钟
原文
文章信息与版本

文章信息

发布
2025年11月11日
收录
2026年8月6日
更新
2026年11月17日
篇幅
约 6 分钟
获赞
4

版本

  1. v1当前

闲话

帅炸了。

这是主播被 n=1n=1 的 case 卡爆了,望周知。

题意

给定长度为 nn 的序列 aa,值域为 [1,n][1,n],有一些位置未确定。你需要给这些未确定的位置的确定取值,使得序列 bi=aaib_i=a_{a_i} 的字典序最小。多测,1≤T≤1051\leq T\leq 10^5,1≤n≤∑n≤5×1051\leq n\leq\sum{n}\leq 5\times 10^5。

题解

首先特判掉一些 corner case。

若 a1=−1∨a1=1a_1=-1\lor a_1=1,我们让所有 −1-1 的位置都填 11 显然最优。

若 aa 中恰好有 11 个 −1-1 位于 pospos 处,考虑若存在 ii 使得 ai>i∧ai=posa_i>i\land a_i=pos,我们就令 apos←1a_{pos}\gets 1;否则我们令 aposa_{pos} 为最靠前的全局最小值的位置(注意这里求全局最小值时要先令 apos←posa_{pos}\gets pos)。这是因为,如果存在 ii 使得 ai>i∧ai=posa_i>i\land a_i=pos,那么 bib_i 要比 bposb_{pos} 更靠前,所以我们肯定优先把 bib_i 置为 11;否则我们就要让 bposb_{pos} 尽可能小,在此基础上,考虑到后面的一些满足 ai=posa_i=pos 的位置,我们还要令 aposa_{pos} 尽可能小,于是 pospos 自然就取最靠前的全局最小值的位置了。

接下来考虑 aa 中至少有 22 个 −1-1 的情况。

首先和前面一种 case 类似,若存在 ii 使得 ai>i∧aai=−1a_i>i\land a_{a_i}=-1,则令 aai←1a_{a_i}\gets 1。

然后考虑所有 ai=−1a_i=-1 的位置,不妨找出 aa 中第一个 11 的位置 pp,∀ai=−1,ai←p\forall a_i=-1,a_i\gets p。若不存在 ai=1a_i=1,则令 pp 为最后一个 ai=−1a_i=-1 的位置,强制令 ap←1a_p\gets 1。这样子除了最后一个位置,每个 −1-1 对应的 bb 都能取到 11,比较优秀。

但是可以发现,这样没有考虑到那些 ai≠−1∧ai<i∧aai=−1a_i\neq -1\land a_i<i\land a_{a_i}=-1 的 ii 对应的 bib_i 的取值,换句话说,我们还要尽可能最小化 aia_i。怎样可以进一步最小化 aia_i 呢?可以发现,我们只能考虑提前把某个 −1-1 的位置置为 11。考虑最小的 ii 使得 ai≠−1∧ai<i∧aai=−1a_i\neq -1\land a_i<i\land a_{a_i}=-1,那么显然 ii 之前的 −1-1 不能动,于是考虑 ii 之后第一个 jj 满足 aj=−1a_j=-1,我们可以尝试把这个 aja_j 置为 11。具体来说,我们直接令前文中找到的 pp 和这个 jj 取 min⁡\min 即可。

依照上述讨论实现即可。时间复杂度为 O(∑n)\mathcal{O}(\sum n)。

代码
C++
#include <bits/stdc++.h>

using namespace std;

#define lowbit(x) ((x) & -(x))
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pii;
const int N = 5e5 + 5;

template<typename T> inline void chk_min(T &x, T y) { x = min(x, y); }
template<typename T> inline void chk_max(T &x, T y) { x = max(x, y); }

int T, n, cnt, a[N];

int main() {
	ios::sync_with_stdio(0), cin.tie(0);
	cin >> T;
	while (T--) {
		cin >> n, cnt = 0;
		for (int i = 1; i <= n; ++i) cin >> a[i], cnt += a[i] == -1;
		if (cnt == 1) {
			int pos = 0, mnp = 0; bool flag = 0;
			for (int i = n; i; --i) {
				if (a[i] == -1) pos = i;
				else {
					if (a[i] == pos) { flag = 1; break; }
					if (!mnp || a[i] <= a[mnp]) mnp = i;
				}
			}
			if (!mnp) mnp = pos;
			if (pos < a[mnp] || (pos == a[mnp] && pos < mnp)) mnp = pos;
			a[pos] = flag ? 1 : mnp;
		} else if (a[1] == -1 || a[1] == 1) {
			for (int i = 1; i <= n; ++i) if (a[i] == -1) a[i] = 1;
		} else if (cnt) {
			for (int i = 1; i <= n; ++i) if (a[i] > i && a[a[i]] == -1) a[a[i]] = 1;
			int pos = 1;
			for (int i = 1; i <= n; ++i)
				if (a[i] == 1) { pos = i; break; }
				else if (a[i] == -1) pos = i;
			for (int i = 1; i <= n; ++i) if (~a[i] && a[i] < i && a[a[i]] == -1) {
				int p = i + 1;
				while (p <= n && ~a[p]) ++p;
				chk_min(pos, p); break;
			}
			for (int i = 1; i <= n; ++i) if (a[i] == -1) a[i] = pos;
			a[pos] = 1;
		}
		for (int i = 1; i <= n; ++i) cout << a[a[i]] << " \n"[i == n];
	}
	return 0;
}
原文