复苏AI (CI翻译)
Consider a permutation $p$ of $1, 2, \ldots, n$. Let $s_i$ denote the number of inversions in the entire prefix $p_1, p_2, \ldots, p_i$, defined as:
考虑一个由 $1, 2, \ldots, n$ 组成的排列 $p$。令 $s_i$ 表示整个前缀 $p_1, p_2, \ldots, p_i$ 中的逆序对数量,定义如下:AI (CI翻译)
$$ s_i = \sum_{1 \le x \lt y \le i} [p_x \gt p_y], $$
$$ s_i = \sum_{1 \le x \lt y \le i} [p_x \gt p_y], $$AI (CI翻译)
where the square parantheses denote the Iverson bracket notation.
其中方括号表示 Iverson 括号记号。AI (CI翻译)
For each position $i$ ($1 \le i \le n$), you are given a condition in the form of either $p_i = x$ or $s_i = x$. Your task is to reconstruct the original permutation $p$.
对于每个位置 $i$($1 \le i \le n$),给定一个条件,形式为 $p_i = x$ 或 $s_i = x$。你的任务是还原原排列 $p$。AI (CI翻译)
Input
输入格式
AI (CI翻译)Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows.
每个输入包含多组测试用例。第一行包含测试用例数量 $t$($1 \le t \le 10^4$)。接下来是各测试用例的描述。AI (CI翻译)
For each test case: The first line contains a single integer $n$ ($1 \le n \le 2\cdot 10^5$). Each of the following $n$ lines contains a character $c$ ($c \in \{\text{'p'}, \text{'s'}\}$) and an integer $x$:
对于每个测试用例:第一行包含一个整数 $n$($1 \le n \le 2\cdot 10^5$)。接下来的 $n$ 行,每行包含一个字符 $c$($c \in \{\text{'p'}, \text{'s'}\}$)和一个整数 $x$:AI (CI翻译)
- If
$c = \text{'p'}$, it denotes that$p_i = x$, where$1 \le x \le n$. - If
$c = \text{'s'}$, it denotes that$s_i = x$, where$0 \le x \le \frac{i(i-1)}{2}$.
- 若
$c = \text{'p'}$,表示$p_i = x$,其中$1 \le x \le n$。 - 若
$c = \text{'s'}$,表示$s_i = x$,其中$0 \le x \le \frac{i(i-1)}{2}$。
The sum of $n$ over all test cases does not exceed $2\cdot 10^5$.
所有测试用例的 $n$ 之和不超过 $2\cdot 10^5$。AI (CI翻译)
It is guaranteed that a valid permutation always exists.
保证一定存在一个合法排列。AI (CI翻译)
Output
输出格式
AI (CI翻译)For each test case, output $n$ integers representing the permutation $p$.
对于每个测试用例,输出 $n$ 个整数,表示排列 $p$。AI (CI翻译)
If there are multiple valid permutations, you can output any of them.
如果有多个合法排列,可以输出任意一个。AI (CI翻译)
Note
说明
AI (CI翻译)In the first test case, the values of all elements are explicitly given, so the unique valid permutation is $\{1, 2, 3\}$.
在第一个测试用例中,所有元素的值都已明确给出,因此唯一合法排列为 $\{1, 2, 3\}$。AI (CI翻译)
In the second test case, we need to reconstruct the permutation from the prefix inversion counts:
在第二个测试用例中,需要根据前缀逆序对数量还原排列:AI (CI翻译)
- For position
$2$, the number of inversions increases by$s_2 - s_1 = 1 - 0 = 1$. This means$p_2$must be smaller than exactly one element before it, so$p_1 \gt p_2$. - For position
$3$, the number of inversions increases by$s_3 - s_2 = 2 - 1 = 1$. This means$p_3$is smaller than exactly one element before it.
- 对于位置
$2$,逆序对数量增加了$s_2 - s_1 = 1 - 0 = 1$。这表示$p_2$恰好比前面的一个元素小,因此$p_1 \gt p_2$。 - 对于位置
$3$,逆序对数量增加了$s_3 - s_2 = 2 - 1 = 1$。这表示$p_3$恰好比前面的一个元素小。
The only permutation satisfying these conditions is $\{3, 1, 2\}$.
唯一满足这些条件的排列是 $\{3, 1, 2\}$。AI (CI翻译)
In the third test case, we are given $p_1 = 1$ and $p_3 = 2$. The only remaining available value for $p_2$ is $3$. We can verify that the prefix $p_1, p_2$ (which is $1, 3$) has $0$ inversions, perfectly satisfying the condition $s_2 = 0$. Thus, the answer is $\{1, 3, 2\}$.
在第三个测试用例中,已知 $p_1 = 1$ 且 $p_3 = 2$。此时 $p_2$ 唯一可用的值是 $3$。可以验证,前缀 $p_1, p_2$(即 $1, 3$)包含 $0$ 个逆序对,符合条件 $s_2 = 0$。因此答案是 $\{1, 3, 2\}$。AI (CI翻译)
In the fifth test case, the given $s_i$ values perfectly match $\frac{i(i-1)}{2}$, which is the maximum possible number of inversions for a prefix of length $i$. This implies that every element must be smaller than all elements before it, meaning the permutation is strictly decreasing. Hence, the answer is $\{6, 5, 4, 3, 2, 1\}$.
在第五个测试用例中,给定的 $s_i$ 值都恰好等于 $\frac{i(i-1)}{2}$,这是长度为 $i$ 的前缀可能具有的最大逆序对数量。这意味着每个元素都必须小于它前面的所有元素,因此排列严格递减。答案为 $\{6, 5, 4, 3, 2, 1\}$。AI (CI翻译)
样例
5
3
p 1
p 2
p 3
3
s 0
s 1
s 2
3
p 1
s 0
p 2
5
p 1
p 4
s 0
p 2
s 4
6
s 0
s 1
s 3
s 6
s 10
s 15
1 2 3
3 1 2
1 3 2
1 4 5 2 3
6 5 4 3 2 1
题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(gpt-6.1-sol),如有出入请以原文为准。