CF1896E.Permutation Sorting

提高+/省选-

通过率:0%

时间限制:4.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

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

  • Let s1,s2,…,sks_1,s_2,\ldots,s_k be the indices of aa that are not good in increasing order. That is, sj<sj+1s_j \lt s_{j+1} and if index ii is not good, then there exists jj such that sj=is_j=i.
  • For each ii from 11 to kk, we assign as(i%k+1):=asia_{s_{(i \% k+1)}} := a_{s_i} all at once.

For each ii from 11 to nn, find the first time that index ii becomes good.

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

给你一个长度为 nn 的排列†^\dagger aa。我们称下标 ii 是“好的”,当且仅当满足 ai=ia_i = i。每过一秒,我们将所有“不好的”下标整体向右循环移动一位。形式化地定义如下:

  • 设 s1,s2,…,sks_1, s_2, \ldots, s_k 是数组 aa 中所有“不好的”下标,按升序排列。即对所有 jj 满足 sj<sj+1s_j < s_{j+1},且若下标 ii 不好,则存在某个 jj 使得 sj=is_j = i。
  • 对每个 ii 从 11 到 kk,同时执行赋值操作:as(i mod k+1):=asia_{s_{(i \bmod k + 1)}} := a_{s_i}。

对每个 ii(从 11 到 nn),求下标 ii 首次变为“好”的时刻(以秒为单位)。

†^\dagger 排列是指由 11 到 nn 这 nn 个互不相同的整数以任意顺序组成的数组。例如,[2,3,1,5,4][2,3,1,5,4] 是一个排列;而 [1,2,2][1,2,2] 不是排列(数字 22 出现了两次),[1,3,4][1,3,4] 也不是排列(此时 n=3n = 3,但数组中出现了 44)。

输入格式

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.

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

The second line of each test case contains nn integers a1,a2,…,ana_1, a_2, \ldots, a_n (1≤ai≤n1 \le a_i \le n) — the elements of permutation aa.

It is guaranteed that the sum of nn over all test cases does not exceed 10610^6.

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

每个测试用例的第一行包含一个整数 nn(1≤n≤1061 \le n \le 10^6)—— 排列 aa 的大小。

每个测试用例的第二行包含 nn 个整数 a1,a2,…,ana_1, a_2, \ldots, a_n(1≤ai≤n1 \le a_i \le n)—— 排列 aa 的元素。

保证所有测试用例的 nn 之和不超过 10610^6。

输出格式

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

对于每个测试用例,输出一行包含 nn 个整数,其中第 ii 个整数表示下标 ii 首次变为“好”的时刻。

输入输出样例

  • 输入#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, 22 and 55 are already in the correct position so indices 22 and 55 become good at 00 second. After 11 second, a cyclic shift will be done with s=[1,3,4]s=[1, 3, 4], resulting in array a=[1,2,3,4,5]a=[1, 2, 3, 4, 5]. Notice that indices 11, 33 and 44 become good at 11 second.

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

在第一个测试用例中,22 和 55 已处于正确位置,因此下标 22 和 55 在 00 秒时即变为“好下标”。经过 11 秒后,将对子数组 s=[1,3,4]s=[1, 3, 4] 执行循环移位操作,得到数组 a=[1,2,3,4,5]a=[1, 2, 3, 4, 5]。注意,下标 11、33 和 44 在 11 秒时变为“好下标”。

在第二个测试用例中,55 已处于正确位置,因此下标 55 在 00 秒时即变为“好下标”。经过 11 秒后,将对子数组 s=[1,2,3,4,6]s=[1, 2, 3, 4, 6] 执行循环移位操作,得到数组 a=[3,2,1,4,5,6]a=[3, 2, 1, 4, 5, 6]。注意,下标 22、44 和 66 在 11 秒时变为“好下标”。经过 22 秒后,将对子数组 s=[1,3]s=[1, 3] 执行循环移位操作,得到数组 a=[1,2,3,4,5,6]a=[1, 2, 3, 4, 5, 6]。注意,下标 11 和 33 在 22 秒时变为“好下标”。

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

首页