CF1650D.Twist the Permutation

普及-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

Petya got an array aa of numbers from 11 to nn, where a[i]=ia[i]=i.

He performed nn operations sequentially. In the end, he received a new state of the aa array.

At the ii-th operation, Petya chose the first ii elements of the array and cyclically shifted them to the right an arbitrary number of times (elements with indexes i+1i+1 and more remain in their places). One cyclic shift to the right is such a transformation that the array a=[a1,a2,…,an]a=[a_1, a_2, \dots, a_n] becomes equal to the array a=[ai,a1,a2,…,ai−2,ai−1,ai+1,ai+2,…,an]a = [a_i, a_1, a_2, \dots, a_{i-2}, a_{i-1}, a_{i+1}, a_{i+2}, \dots, a_n].

For example, if a=[5,4,2,1,3]a = [5,4,2,1,3] and i=3i=3 (that is, this is the third operation), then as a result of this operation, he could get any of these three arrays:

  • a=[5,4,2,1,3]a = [5,4,2,1,3] (makes 00 cyclic shifts, or any number that is divisible by 33);
  • a=[2,5,4,1,3]a = [2,5,4,1,3] (makes 11 cyclic shift, or any number that has a remainder of 11 when divided by 33);
  • a=[4,2,5,1,3]a = [4,2,5,1,3] (makes 22 cyclic shifts, or any number that has a remainder of 22 when divided by 33).

Let's look at an example. Let n=6n=6, i.e. initially a=[1,2,3,4,5,6]a=[1,2,3,4,5,6]. A possible scenario is described below.

  • i=1i=1: no matter how many cyclic shifts Petya makes, the array aa does not change.
  • i=2i=2: let's say Petya decided to make a 11 cyclic shift, then the array will look like a=[2,1,3,4,5,6]a = [\textbf{2}, \textbf{1}, 3, 4, 5, 6].
  • i=3i=3: let's say Petya decided to make 11 cyclic shift, then the array will look like a=[3,2,1,4,5,6]a = [\textbf{3}, \textbf{2}, \textbf{1}, 4, 5, 6].
  • i=4i=4: let's say Petya decided to make 22 cyclic shifts, the original array will look like a=[1,4,3,2,5,6]a = [\textbf{1}, \textbf{4}, \textbf{3}, \textbf{2}, 5, 6].
  • i=5i=5: let's say Petya decided to make 00 cyclic shifts, then the array won't change.
  • i=6i=6: let's say Petya decided to make 44 cyclic shifts, the array will look like a=[3,2,5,6,1,4]a = [\textbf{3}, \textbf{2}, \textbf{5}, \textbf{6}, \textbf{1}, \textbf{4}].

You are given a final array state aa after all nn operations. Determine if there is a way to perform the operation that produces this result. In this case, if an answer exists, print the numbers of cyclical shifts that occurred during each of the nn operations.

佩佳得到了一个由 11 到 nn 的数字组成的数组 aa,其中 a[i]=ia[i]=i。

他依次执行了 nn 次操作。最终,他得到了数组 aa 的一种新状态。

在第 ii 次操作中,佩佳选取数组的前 ii 个元素,并将其向右循环移动任意次数(下标为 i+1i+1 及之后的元素保持不变)。一次向右的循环移动是指将数组 a=[a1,a2,…,an]a=[a_1, a_2, \dots, a_n] 变为 a=[ai,a1,a2,…,ai−2,ai−1,ai+1,ai+2,…,an]a = [a_i, a_1, a_2, \dots, a_{i-2}, a_{i-1}, a_{i+1}, a_{i+2}, \dots, a_n]。

例如,若 a=[5,4,2,1,3]a = [5,4,2,1,3] 且 i=3i=3(即这是第三次操作),则该操作后可能得到以下三个数组之一:

  • a=[5,4,2,1,3]a = [5,4,2,1,3](执行 00 次循环移动,或任意被 33 整除的次数);
  • a=[2,5,4,1,3]a = [2,5,4,1,3](执行 11 次循环移动,或任意模 33 余 11 的次数);
  • a=[4,2,5,1,3]a = [4,2,5,1,3](执行 22 次循环移动,或任意模 33 余 22 的次数)。

我们来看一个例子。设 n=6n=6,即初始时 a=[1,2,3,4,5,6]a=[1,2,3,4,5,6]。下面描述了一种可能的操作过程:

  • i=1i=1:无论佩佳执行多少次循环移动,数组 aa 均不发生变化。
  • i=2i=2:假设佩佳决定执行 11 次循环移动,则数组变为 a=[2,1,3,4,5,6]a = [\textbf{2}, \textbf{1}, 3, 4, 5, 6]。
  • i=3i=3:假设佩佳决定执行 11 次循环移动,则数组变为 a=[3,2,1,4,5,6]a = [\textbf{3}, \textbf{2}, \textbf{1}, 4, 5, 6]。
  • i=4i=4:假设佩佳决定执行 22 次循环移动,则数组变为 a=[1,4,3,2,5,6]a = [\textbf{1}, \textbf{4}, \textbf{3}, \textbf{2}, 5, 6]。
  • i=5i=5:假设佩佳决定执行 00 次循环移动,则数组不变。
  • i=6i=6:假设佩佳决定执行 44 次循环移动,则数组变为 a=[3,2,5,6,1,4]a = [\textbf{3}, \textbf{2}, \textbf{5}, \textbf{6}, \textbf{1}, \textbf{4}]。

现给出所有 nn 次操作后的最终数组状态 aa。请判断是否存在一种操作序列能产生该结果。若存在,请输出每次操作所执行的循环移动次数(共 nn 个整数)。

输入格式

The first line of the input contains an integer tt (1≤t≤5001 \le t \le 500) — the number of test cases in the test.

The descriptions of the test cases follow.

The first line of the description of each test case contains one integer nn (2≤n≤2⋅1032 \le n \le 2\cdot10^3) — the length of the array aa.

The next line contains the final state of the array aa: nn integers a1,a2,…,ana_1, a_2, \dots, a_n (1≤ai≤n1 \le a_i \le n) are written. All aia_i are distinct.

It is guaranteed that the sum of nn values over all test cases does not exceed 2⋅1032\cdot10^3.

输入的第一行包含一个整数 tt(1≤t≤5001 \le t \le 500),表示测试用例的数量。

随后是各测试用例的描述。

每个测试用例的描述以一行开始,该行包含一个整数 nn(2≤n≤2⋅1032 \le n \le 2\cdot10^3),表示数组 aa 的长度。

下一行包含数组 aa 的最终状态:nn 个整数 a1,a2,…,ana_1, a_2, \dots, a_n(1≤ai≤n1 \le a_i \le n)。所有 aia_i 互不相同。

保证所有测试用例中 nn 的总和不超过 2⋅1032\cdot10^3。

输出格式

For each test case, print the answer on a separate line.

Print -1 if the given final value aa cannot be obtained by performing an arbitrary number of cyclic shifts on each operation. Otherwise, print nn non-negative integers d1,d2,…,dnd_1, d_2, \dots, d_n (di≥0d_i \ge 0), where did_i means that during the ii-th operation the first ii elements of the array were cyclic shifted to the right did_i times.

If there are several possible answers, print the one where the total number of shifts is minimal (that is, the sum of did_i values is the smallest). If there are several such answers, print any of them.

对于每个测试用例,请在单独的一行上输出答案。

如果通过任意次数的循环移位操作均无法得到给定的最终值 aa,则输出 −1-1。否则,输出 nn 个非负整数 d1,d2,…,dnd_1, d_2, \dots, d_n(其中 di≥0d_i \ge 0),其中 did_i 表示在第 ii 次操作中,将数组的前 ii 个元素向右循环移位 did_i 次。

若存在多个可能的答案,请输出总移位次数最少的那个(即所有 did_i 之和最小)。若仍存在多个满足该条件的答案,则输出其中任意一个即可。

输入输出样例

  • 输入#1

    3
    6
    3 2 5 6 1 4
    3
    3 1 2
    8
    5 8 1 3 2 6 4 7

    输出#1

    0 1 1 2 0 4 
    0 0 1 
    0 1 2 0 2 5 6 2

说明/提示

The first test case matches the example from the statement.

The second set of input data is simple. Note that the answer [3,2,1][3, 2, 1] also gives the same permutation, but since the total number of shifts 3+2+13+2+1 is greater than 0+0+10+0+1, this answer is not correct.

第一个测试用例与题面中的示例一致。

第二组输入数据较为简单。注意:答案 [3,2,1][3, 2, 1] 同样能生成相同的排列,但由于总移位次数 3+2+13+2+1 大于 0+0+10+0+1,因此该答案不正确。

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

首页