CF1896E.Permutation Sorting
普及/提高-
通过率:0%
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
You are given a permutation † a of size n . We call an index i good if ai=i is satisfied. After each second, we rotate all indices that are not good to the right by one position. Formally,
- Let s1,s2,…,sk be the indices of a that are not good in increasing order. That is, sj<sj+1 and if index i is not good, then there exists j such that sj=i .
- For each i from 1 to k , we assign as(i%k+1):=asi all at once.
For each i from 1 to n , find the first time that index i becomes good.
† 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).
输入格式
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.
The first line of each test case contains a single integer n ( 1≤n≤106 ) — the size of permutation a .
The second line of each test case contains n integers a1,a2,…,an ( 1≤ai≤n ) — the elements of permutation a .
It is guaranteed that the sum of n over all test cases does not exceed 106 .
输出格式
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.
输入输出样例
输入#1
2 5 3 2 4 1 5 6 2 1 4 6 5 3
输出#1
1 0 1 1 0 2 1 2 1 0 1
说明/提示
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.