310836: CF1896E. Permutation Sorting

Memory Limit:256 MB Time Limit:4 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

E. Permutation Sortingtime limit per test4 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard output

You are given a permutation$^\dagger$ $a$ of size $n$. We call an index $i$ good if $a_i=i$ is satisfied. After each second, we rotate all indices that are not good to the right by one position. Formally,

  • Let $s_1,s_2,\ldots,s_k$ be the indices of $a$ that are not good in increasing order. That is, $s_j < s_{j+1}$ and if index $i$ is not good, then there exists $j$ such that $s_j=i$.
  • For each $i$ from $1$ to $k$, we assign $a_{s_{(i \% k+1)}} := a_{s_i}$ all at once.

For each $i$ from $1$ to $n$, find the first time that index $i$ becomes good.

$^\dagger$ A permutation is an array consisting of $n$ distinct integers from $1$ to $n$ in arbitrary order. For example, $[2,3,1,5,4]$ is a permutation, but $[1,2,2]$ is not a permutation ($2$ appears twice in the array) and $[1,3,4]$ is also not a permutation ($n=3$ but there is $4$ in the array).

Input

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.

The first line of each test case contains a single integer $n$ ($1 \le n \le 10^6$) — the size of permutation $a$.

The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le n$) — the elements of permutation $a$.

It is guaranteed that the sum of $n$ over all test cases does not exceed $10^6$.

Output

For each test case, output a single line containing $n$ integers where the $i$-th integer represents the first time that index $i$ becomes good.

ExampleInput
2
5
3 2 4 1 5
6
2 1 4 6 5 3
Output
1 0 1 1 0 
2 1 2 1 0 1 
Note

In the first test case, $2$ and $5$ are already in the correct position so indices $2$ and $5$ become good at $0$ second. After $1$ second, a cyclic shift will be done with $s=[1, 3, 4]$, resulting in array $a=[1, 2, 3, 4, 5]$. Notice that indices $1$, $3$ and $4$ become good at $1$ second.

In the second test case, $5$ is already in the correct position, so index $5$ becomes good at $0$ second. After $1$ second, a cyclic shift will be done with $s=[1, 2, 3, 4, 6]$, resulting in array $a=[3, 2, 1, 4, 5, 6]$. Notice that indices $2$, $4$ and $6$ become good at $1$ second. After $2$ seconds, a cyclic shift will be done with $s=[1, 3]$, resulting in array $a=[1, 2, 3, 4, 5, 6]$. Notice that indices $1$ and $3$ become good at $2$ second.

Output

题目大意:
题目是关于对排列进行排序的问题。给定一个大小为 $ n $ 的排列 $ a $,一个索引 $ i $ 如果满足 $ a_i = i $,则称这个索引是“好的”。每秒钟,我们将所有不是“好的”的索引向右旋转一个位置。具体来说,找到所有不是“好的”的索引,将它们按升序排列,然后一次性将每个索引的值赋给它的下一个索引。对于每个 $ i $ 从 $ 1 $ 到 $ n $,找出索引 $ i $ 变成“好的”的第一个时间。

输入输出数据格式:
输入:
- 第一行包含一个整数 $ t $($ 1 \le t \le 10^4 $),表示测试用例的数量。
- 每个测试用例包含两行:
- 第一行包含一个整数 $ n $($ 1 \le n \le 10^6 $),表示排列 $ a $ 的大小。
- 第二行包含 $ n $ 个整数 $ a_1, a_2, \ldots, a_n $($ 1 \le a_i \le n $),表示排列 $ a $ 的元素。
- 保证所有测试用例的 $ n $ 之和不超过 $ 10^6 $。

输出:
- 对于每个测试用例,输出一行包含 $ n $ 个整数,其中第 $ i $ 个整数表示索引 $ i $ 变成“好的”的第一个时间。

注意:
- 排列是一个由 $ n $ 个从 $ 1 $ 到 $ n $ 的不同整数以任意顺序组成的数组。例如,$ [2,3,1,5,4] $ 是一个排列,但 $ [1,2,2] $ 不是排列(因为数组中 $ 2 $ 出现了两次),$ [1,3,4] $ 也不是排列(因为 $ n=3 $ 但数组中有 $ 4 $)。

示例输入:
```
2
5
3 2 4 1 5
6
2 1 4 6 5 3
```

示例输出:
```
1 0 1 1 0
2 1 2 1 0 1
```

解释:
- 在第一个测试用例中,2 和 5 已经在正确的位置,所以索引 2 和 5 在 0 秒时变成“好的”。在 1 秒时,对索引 [1, 3, 4] 进行循环移位,得到数组 [1, 2, 3, 4, 5]。注意索引 1、3 和 4 在 1 秒时变成“好的”。
- 在第二个测试用例中,5 已经在正确的位置,所以索引 5 在 0 秒时变成“好的”。在 1 秒时,对索引 [1, 2, 3, 4, 6] 进行循环移位,得到数组 [3, 2, 1, 4, 5, 6]。注意索引 2、4 和 6 在 1 秒时变成“好的”。在 2 秒时,对索引 [1, 3] 进行循环移位,得到数组 [1, 2, 3, 4, 5, 6]。注意索引 1 和 3 在 2 秒时变成“好的”。题目大意: 题目是关于对排列进行排序的问题。给定一个大小为 $ n $ 的排列 $ a $,一个索引 $ i $ 如果满足 $ a_i = i $,则称这个索引是“好的”。每秒钟,我们将所有不是“好的”的索引向右旋转一个位置。具体来说,找到所有不是“好的”的索引,将它们按升序排列,然后一次性将每个索引的值赋给它的下一个索引。对于每个 $ i $ 从 $ 1 $ 到 $ n $,找出索引 $ i $ 变成“好的”的第一个时间。 输入输出数据格式: 输入: - 第一行包含一个整数 $ t $($ 1 \le t \le 10^4 $),表示测试用例的数量。 - 每个测试用例包含两行: - 第一行包含一个整数 $ n $($ 1 \le n \le 10^6 $),表示排列 $ a $ 的大小。 - 第二行包含 $ n $ 个整数 $ a_1, a_2, \ldots, a_n $($ 1 \le a_i \le n $),表示排列 $ a $ 的元素。 - 保证所有测试用例的 $ n $ 之和不超过 $ 10^6 $。 输出: - 对于每个测试用例,输出一行包含 $ n $ 个整数,其中第 $ i $ 个整数表示索引 $ i $ 变成“好的”的第一个时间。 注意: - 排列是一个由 $ n $ 个从 $ 1 $ 到 $ n $ 的不同整数以任意顺序组成的数组。例如,$ [2,3,1,5,4] $ 是一个排列,但 $ [1,2,2] $ 不是排列(因为数组中 $ 2 $ 出现了两次),$ [1,3,4] $ 也不是排列(因为 $ n=3 $ 但数组中有 $ 4 $)。 示例输入: ``` 2 5 3 2 4 1 5 6 2 1 4 6 5 3 ``` 示例输出: ``` 1 0 1 1 0 2 1 2 1 0 1 ``` 解释: - 在第一个测试用例中,2 和 5 已经在正确的位置,所以索引 2 和 5 在 0 秒时变成“好的”。在 1 秒时,对索引 [1, 3, 4] 进行循环移位,得到数组 [1, 2, 3, 4, 5]。注意索引 1、3 和 4 在 1 秒时变成“好的”。 - 在第二个测试用例中,5 已经在正确的位置,所以索引 5 在 0 秒时变成“好的”。在 1 秒时,对索引 [1, 2, 3, 4, 6] 进行循环移位,得到数组 [3, 2, 1, 4, 5, 6]。注意索引 2、4 和 6 在 1 秒时变成“好的”。在 2 秒时,对索引 [1, 3] 进行循环移位,得到数组 [1, 2, 3, 4, 5, 6]。注意索引 1 和 3 在 2 秒时变成“好的”。

加入题单

上一题 下一题 算法标签: