CF2239C.Revival

提高+/省选-

通过率:0%

时间限制:2.50s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

Consider a permutation pp of 1,2,…,n1, 2, \ldots, n. Let sis_i denote the number of inversions in the entire prefix p1,p2,…,pip_1, p_2, \ldots, p_i, 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 ii (1≤i≤n1 \le i \le n), you are given a condition in the form of either pi=xp_i = x or si=xs_i = x. Your task is to reconstruct the original permutation pp.

考虑 1,2,…,n1, 2, \ldots, n 的一个排列 pp。令 sis_i 表示整个前缀 p1,p2,…,pip_1, p_2, \ldots, p_i 中的逆序对数量,定义为:

s\_i = \\sum\_{1 \\le x \\lt y \\le i} \[p\_x \\gt p\_y\],

其中方括号表示 Iverson bracket 记号。

对每个位置 ii(1≤i≤n1 \le i \le n),你将获得一个形如 pi=xp_i = x 或 si=xs_i = x 的条件。你的任务是重构原始排列 pp。

输入格式

Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤1041 \le t \le 10^4). The description of the test cases follows.

For each test case: The first line contains a single integer nn (1≤n≤2⋅1051 \le n \le 2\cdot 10^5). Each of the following nn lines contains a character cc (c∈’p’,’s’c \in {\text{'p'}, \text{'s'}}) and an integer xx:

  • If c=’p’c = \text{'p'}, it denotes that pi=xp_i = x, where 1≤x≤n1 \le x \le n.
  • If c=’s’c = \text{'s'}, it denotes that si=xs_i = x, where 0≤x≤i(i−1)20 \le x \le \frac{i(i-1)}{2}.

The sum of nn over all test cases does not exceed 2⋅1052\cdot 10^5.

It is guaranteed that a valid permutation always exists.

每个测试包含多个测试用例。第一行包含测试用例的数量 tt(1≤t≤1041 \le t \le 10^4)。随后是各测试用例的描述。

对于每个测试用例:第一行包含一个整数 nn(1≤n≤2⋅1051 \le n \le 2\cdot 10^5)。接下来的 nn 行,每行包含一个字符 cc(c∈’p’,’s’c \in {\text{'p'}, \text{'s'}})和一个整数 xx:

  • 若 c=’p’c = \text{'p'},表示 pi=xp_i = x,其中 1≤x≤n1 \le x \le n;
  • 若 c=’s’c = \text{'s'},表示 si=xs_i = x,其中 0≤x≤i(i−1)20 \le x \le \frac{i(i-1)}{2}。

所有测试用例中 nn 的总和不超过 2⋅1052\cdot 10^5。

保证始终存在合法的排列。

输出格式

For each test case, output nn integers representing the permutation pp.

If there are multiple valid permutations, you can output any of them.

对于每个测试用例,输出 nn 个整数,表示排列 pp。

如果存在多个合法的排列,你可以输出其中任意一个。

输入输出样例

  • 输入#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{1, 2, 3}.

In the second test case, we need to reconstruct the permutation from the prefix inversion counts:

  • For position 22, the number of inversions increases by s2−s1=1−0=1s_2 - s_1 = 1 - 0 = 1. This means p2p_2 must be smaller than exactly one element before it, so p1>p2p_1 \gt p_2.
  • For position 33, the number of inversions increases by s3−s2=2−1=1s_3 - s_2 = 2 - 1 = 1. This means p3p_3 is smaller than exactly one element before it.

The only permutation satisfying these conditions is 3,1,2{3, 1, 2}.

In the third test case, we are given p1=1p_1 = 1 and p3=2p_3 = 2. The only remaining available value for p2p_2 is 33. We can verify that the prefix p1,p2p_1, p_2 (which is 1,31, 3) has 00 inversions, perfectly satisfying the condition s2=0s_2 = 0. Thus, the answer is 1,3,2{1, 3, 2}.

In the fifth test case, the given sis_i values perfectly match i(i−1)2\frac{i(i-1)}{2}, which is the maximum possible number of inversions for a prefix of length ii. 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{6, 5, 4, 3, 2, 1}.

在第一个测试用例中,所有元素的值均被显式给出,因此唯一有效的排列是 1,2,3{1, 2, 3}。

在第二个测试用例中,我们需要根据前缀逆序数(prefix inversion counts)重构排列:

  • 在位置 22 处,逆序数增加量为 s2−s1=1−0=1s_2 - s_1 = 1 - 0 = 1。这意味着 p2p_2 恰好比它前面的一个元素小,即 p1>p2p_1 \gt p_2。
  • 在位置 33 处,逆序数增加量为 s3−s2=2−1=1s_3 - s_2 = 2 - 1 = 1。这意味着 p3p_3 恰好比它前面的一个元素小。

唯一满足这些条件的排列是 3,1,2{3, 1, 2}。

在第三个测试用例中,已知 p1=1p_1 = 1 且 p3=2p_3 = 2。p2p_2 唯一剩余可用的值是 33。我们可以验证:前缀 p1,p2p_1, p_2(即 1,31, 3)包含 00 个逆序对,完全满足条件 s2=0s_2 = 0。因此答案为 1,3,2{1, 3, 2}。

在第五个测试用例中,给定的 sis_i 值恰好等于 i(i−1)2\frac{i(i-1)}{2},即长度为 ii 的前缀所能达到的最大逆序数。这表明每个元素都必须小于其之前的所有元素,即该排列严格递减。因此答案为 6,5,4,3,2,1{6, 5, 4, 3, 2, 1}。

输入解题思路,AI测评打分。不知道怎么写?

首页