CF2239C.Revival
提高+/省选-
通过率:0%
时间限制:2.50s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
Consider a permutation p of 1,2,…,n. Let si denote the number of inversions in the entire prefix p1,p2,…,pi, defined as:
s\_i = \\sum\_{1 \\le x \\lt y \\le i} \[p\_x \\gt p\_y\],where the square parantheses denote the Iverson bracket notation.
For each position i (1≤i≤n), you are given a condition in the form of either pi=x or si=x. Your task is to reconstruct the original permutation p.
考虑 1,2,…,n 的一个排列 p。令 si 表示整个前缀 p1,p2,…,pi 中的逆序对数量,定义为:
s\_i = \\sum\_{1 \\le x \\lt y \\le i} \[p\_x \\gt p\_y\],其中方括号表示 Iverson bracket 记号。
对每个位置 i(1≤i≤n),你将获得一个形如 pi=x 或 si=x 的条件。你的任务是重构原始排列 p。
输入格式
Each test contains multiple test cases. The first line contains the number of test cases t (1≤t≤104). The description of the test cases follows.
For each test case: The first line contains a single integer n (1≤n≤2⋅105). Each of the following n lines contains a character c (c∈’p’,’s’) and an integer x:
- If c=’p’, it denotes that pi=x, where 1≤x≤n.
- If c=’s’, it denotes that si=x, where 0≤x≤2i(i−1).
The sum of n over all test cases does not exceed 2⋅105.
It is guaranteed that a valid permutation always exists.
每个测试包含多个测试用例。第一行包含测试用例的数量 t(1≤t≤104)。随后是各测试用例的描述。
对于每个测试用例:第一行包含一个整数 n(1≤n≤2⋅105)。接下来的 n 行,每行包含一个字符 c(c∈’p’,’s’)和一个整数 x:
- 若 c=’p’,表示 pi=x,其中 1≤x≤n;
- 若 c=’s’,表示 si=x,其中 0≤x≤2i(i−1)。
所有测试用例中 n 的总和不超过 2⋅105。
保证始终存在合法的排列。
输出格式
For each test case, output n integers representing the permutation p.
If there are multiple valid permutations, you can output any of them.
对于每个测试用例,输出 n 个整数,表示排列 p。
如果存在多个合法的排列,你可以输出其中任意一个。
输入输出样例
输入#1
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
1 2 3 3 1 2 1 3 2 1 4 5 2 3 6 5 4 3 2 1
说明/提示
In the first test case, the values of all elements are explicitly given, so the unique valid permutation is 1,2,3.
In the second test case, we need to reconstruct the permutation from the prefix inversion counts:
- For position 2, the number of inversions increases by s2−s1=1−0=1. This means p2 must be smaller than exactly one element before it, so p1>p2.
- For position 3, the number of inversions increases by s3−s2=2−1=1. This means p3 is smaller than exactly one element before it.
The only permutation satisfying these conditions is 3,1,2.
In the third test case, we are given p1=1 and p3=2. The only remaining available value for p2 is 3. We can verify that the prefix p1,p2 (which is 1,3) has 0 inversions, perfectly satisfying the condition s2=0. Thus, the answer is 1,3,2.
In the fifth test case, the given si values perfectly match 2i(i−1), 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.
在第一个测试用例中,所有元素的值均被显式给出,因此唯一有效的排列是 1,2,3。
在第二个测试用例中,我们需要根据前缀逆序数(prefix inversion counts)重构排列:
- 在位置 2 处,逆序数增加量为 s2−s1=1−0=1。这意味着 p2 恰好比它前面的一个元素小,即 p1>p2。
- 在位置 3 处,逆序数增加量为 s3−s2=2−1=1。这意味着 p3 恰好比它前面的一个元素小。
唯一满足这些条件的排列是 3,1,2。
在第三个测试用例中,已知 p1=1 且 p3=2。p2 唯一剩余可用的值是 3。我们可以验证:前缀 p1,p2(即 1,3)包含 0 个逆序对,完全满足条件 s2=0。因此答案为 1,3,2。
在第五个测试用例中,给定的 si 值恰好等于 2i(i−1),即长度为 i 的前缀所能达到的最大逆序数。这表明每个元素都必须小于其之前的所有元素,即该排列严格递减。因此答案为 6,5,4,3,2,1。
输入解题思路,AI测评打分。不知道怎么写?