#CF2239C. 复兴 / C. Revival
复兴 / C. Revival
C. Revival
Source: Codeforces 2239C
Contest: Codeforces Round 1105 (Div. 1)
Time limit: 2.5 seconds
Memory limit: 256 megabytes
Statement
Consider a permutation of . Let denote the number of inversions in the entire prefix , defined as:
where the square parantheses denote the Iverson bracket notation.
For each position (), you are given a condition in the form of either or . Your task is to reconstruct the original permutation .
Input
Each test contains multiple test cases. The first line contains the number of test cases (). The description of the test cases follows.
For each test case: The first line contains a single integer (). Each of the following lines contains a character () and an integer :
- If , it denotes that , where .
- If , it denotes that , where .
The sum of over all test cases does not exceed .
It is guaranteed that a valid permutation always exists.
Output
For each test case, output integers representing the permutation .
If there are multiple valid permutations, you can output any of them.
Note
In the first test case, the values of all elements are explicitly given, so the unique valid permutation is .
In the second test case, we need to reconstruct the permutation from the prefix inversion counts:
- For position , the number of inversions increases by . This means must be smaller than exactly one element before it, so .
- For position , the number of inversions increases by . This means is smaller than exactly one element before it.
The only permutation satisfying these conditions is . In the third test case, we are given and . The only remaining available value for is . We can verify that the prefix (which is ) has inversions, perfectly satisfying the condition . Thus, the answer is .
In the fifth test case, the given values perfectly match , which is the maximum possible number of inversions for a prefix of length . This implies that every element must be smaller than all elements before it, meaning the permutation is strictly decreasing. Hence, the answer is .
Examples
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