CF1579B.Shifting Sort

普及-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

The new generation external memory contains an array of integers a[1…n]=[a1,a2,…,an]a[1 \ldots n] = [a_1, a_2, \ldots, a_n].

This type of memory does not support changing the value of an arbitrary element. Instead, it allows you to cut out any segment of the given array, cyclically shift (rotate) it by any offset and insert it back into the same place.

Technically, each cyclic shift consists of two consecutive actions:

  1. You may select arbitrary indices ll and rr (1≤l<r≤n1 \le l \lt r \le n) as the boundaries of the segment.
  2. Then you replace the segment a[l…r]a[l \ldots r] with it's cyclic shift to the left by an arbitrary offset dd. The concept of a cyclic shift can be also explained by following relations: the sequence [1,4,1,3][1, 4, 1, 3] is a cyclic shift of the sequence [3,1,4,1][3, 1, 4, 1] to the left by the offset 11 and the sequence [4,1,3,1][4, 1, 3, 1] is a cyclic shift of the sequence [3,1,4,1][3, 1, 4, 1] to the left by the offset 22.

For example, if a=[1,3,2,8,5]a = [1, \color{blue}{3, 2, 8}, 5], then choosing l=2l = 2, r=4r = 4 and d=2d = 2 yields a segment a[2…4]=[3,2,8]a[2 \ldots 4] = [3, 2, 8]. This segment is then shifted by the offset d=2d = 2 to the left, and you get a segment [8,3,2][8, 3, 2] which then takes the place of of the original elements of the segment. In the end you get a=[1,8,3,2,5]a = [1, \color{blue}{8, 3, 2}, 5].

Sort the given array aa using no more than nn cyclic shifts of any of its segments. Note that you don't need to minimize the number of cyclic shifts. Any method that requires nn or less cyclic shifts will be accepted.

新一代外部存储器包含一个整数数组 a[1…n]=[a1,a2,…,an]a[1 \ldots n] = [a_1, a_2, \ldots, a_n]。

此类存储器不支持修改任意元素的值。相反,它允许你裁剪出给定数组的任意连续子段,以任意偏移量对该子段进行循环移位(旋转),然后将其重新插入到原位置。

从技术上讲,每次循环移位由以下两个连续操作组成:

  1. 你可以任选下标 ll 和 rr(满足 1≤l<r≤n1 \le l \lt r \le n)作为子段的边界;
  2. 然后将子段 a[l…r]a[l \ldots r] 替换为它向左循环移位任意偏移量 dd 后的结果。循环移位的概念可通过如下关系说明:序列 [1,4,1,3][1, 4, 1, 3] 是序列 [3,1,4,1][3, 1, 4, 1] 向左循环移位偏移量 11 的结果;而序列 [4,1,3,1][4, 1, 3, 1] 是序列 [3,1,4,1][3, 1, 4, 1] 向左循环移位偏移量 22 的结果。

例如,若 a=[1,3,2,8,5]a = [1, \color{blue}{3, 2, 8}, 5],选择 l=2l = 2、r=4r = 4 与 d=2d = 2,则得到子段 a[2…4]=[3,2,8]a[2 \ldots 4] = [3, 2, 8]。该子段向左循环移位偏移量 d=2d = 2 后变为 [8,3,2][8, 3, 2],并以此替换原位置上的元素。最终得到 a=[1,8,3,2,5]a = [1, \color{blue}{8, 3, 2}, 5]。

请使用不超过 nn 次任意子段的循环移位操作,对给定数组 aa 进行排序。注意:你无需最小化循环移位的次数;任何使用 nn 次或更少循环移位的操作方法均可接受。

输入格式

The first line contains an integer tt (1≤t≤10001 \leq t \leq 1000) — the number of test cases.

The next 2t2t lines contain the descriptions of the test cases.

The first line of each test case description contains an integer nn (2≤n≤502 \leq n \leq 50) — the length of the array. The second line consists of space-separated elements of the array aia_i (−109≤ai≤109-10^9 \leq a_i \leq 10^9). Elements of array aa may repeat and don't have to be unique.

第一行包含一个整数 tt(1≤t≤10001 \leq t \leq 1000)—— 表示测试用例的数量。

接下来的 2t2t 行包含各测试用例的描述。

每个测试用例描述的第一行包含一个整数 nn(2≤n≤502 \leq n \leq 50)—— 表示数组的长度。第二行包含由空格分隔的数组元素 aia_i(−109≤ai≤109-10^9 \leq a_i \leq 10^9)。数组 aa 中的元素可能重复,不一定是互异的。

输出格式

Print tt answers to all input test cases.

The first line of the answer of each test case should contain an integer kk (0≤k≤n0 \le k \le n) — the number of actions to sort the array. The next kk lines should contain descriptions of the actions formatted as "ll rr dd" (without quotes) where ll and rr (1≤l<r≤n1 \le l \lt r \le n) are the boundaries of the segment being shifted, while dd (1≤d≤r−l1 \le d \le r - l) is the offset value. Please remember that only the cyclic shifts to the left are considered so the chosen segment will be shifted by the offset dd to the to the left.

Note that you are not required to find the minimum number of cyclic shifts needed for sorting. Any sorting method where the number of shifts does not exceed nn will be accepted.

If the given array aa is already sorted, one of the possible answers is k=0k = 0 and an empty sequence of cyclic shifts.

If there are several possible answers, you may print any of them.

输出所有输入测试用例的 tt 个答案。

每个测试用例的答案的第一行应包含一个整数 kk(0≤k≤n0 \le k \le n)—— 表示用于排序数组的操作次数。接下来的 kk 行应描述这些操作,格式为 "ll rr dd"(不含引号),其中 ll 和 rr(1≤l<r≤n1 \le l \lt r \le n)是待移位区间的左右边界,而 dd(1≤d≤r−l1 \le d \le r - l)是偏移量。请注意,此处仅考虑向左的循环移位,即所选区间将向左循环移动 dd 个位置。

注意:你无需找出排序所需的最少循环移位次数。只要移位次数不超过 nn 的任意排序方法均被接受。

若给定数组 aa 已经有序,则一种可能的答案是 k=0k = 0,且不输出任何循环移位操作。

若存在多种可能的答案,你可以输出其中任意一种。

输入输出样例

  • 输入#1

    4
    2
    2 1
    3
    1 2 1
    4
    2 4 1 3
    5
    2 5 1 4 3

    输出#1

    1
    1 2 1
    1
    1 3 2
    3
    2 4 1
    2 3 1
    1 3 2
    4
    2 4 2
    1 5 3
    1 2 1
    1 3 1

说明/提示

Explanation of the fourth data set in the example:

  1. The segment a[2…4]a[2 \ldots 4] is selected and is shifted to the left by 22: [2,5,1,4,3]⟶[2,4,5,1,3][2, \color{blue}{5, 1, 4}, 3] \longrightarrow [2, \color{blue}{4, 5, 1}, 3]
  2. The segment a[1…5]a[1 \ldots 5] is then selected and is shifted to the left by 33: [2,4,5,1,3]⟶[1,3,2,4,5][\color{blue}{2, 4, 5, 1, 3}] \longrightarrow [\color{blue}{1, 3, 2, 4, 5}]
  3. After that the segment a[1…2]a[1 \ldots 2] is selected and is shifted to the left by 11: [1,3,2,4,5]⟶[3,1,2,4,5][\color{blue}{1, 3}, 2, 4, 5] \longrightarrow [\color{blue}{3, 1}, 2, 4, 5]
  4. And in the end the segment a[1…3]a[1 \ldots 3] is selected and is shifted to the left by 11: [3,1,2,4,5]⟶[1,2,3,4,5][\color{blue}{3, 1, 2}, 4, 5] \longrightarrow [\color{blue}{1, 2, 3}, 4, 5]

示例中第四组数据的解释:

  1. 选择子段 a[2…4]a[2 \ldots 4],并将其向左平移 22 位:[2,5,1,4,3]⟶[2,4,5,1,3][2, \color{blue}{5, 1, 4}, 3] \longrightarrow [2, \color{blue}{4, 5, 1}, 3]
  2. 接着选择子段 a[1…5]a[1 \ldots 5],并将其向左平移 33 位:[2,4,5,1,3]⟶[1,3,2,4,5][\color{blue}{2, 4, 5, 1, 3}] \longrightarrow [\color{blue}{1, 3, 2, 4, 5}]
  3. 然后选择子段 a[1…2]a[1 \ldots 2],并将其向左平移 11 位:[1,3,2,4,5]⟶[3,1,2,4,5][\color{blue}{1, 3}, 2, 4, 5] \longrightarrow [\color{blue}{3, 1}, 2, 4, 5]
  4. 最后选择子段 a[1…3]a[1 \ldots 3],并将其向左平移 11 位:[3,1,2,4,5]⟶[1,2,3,4,5][\color{blue}{3, 1, 2}, 4, 5] \longrightarrow [\color{blue}{1, 2, 3}, 4, 5]

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

首页