Algorithm


B. Shifting Sort
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

The new generation external memory contains an array of integers a[1n]=[a1,a2,,an]�[1…�]=[�1,�2,…,��].

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 l and r (1l<rn1≤�<�≤�) as the boundaries of the segment.
  2. Then you replace the segment a[lr]�[�…�] with it's cyclic shift to the left by an arbitrary offset d. 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]�=[1,3,2,8,5], then choosing l=2�=2r=4�=4 and d=2�=2 yields a segment a[24]=[3,2,8]�[2…4]=[3,2,8]. This segment is then shifted by the offset d=2�=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]�=[1,8,3,2,5].

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

Input

The first line contains an integer t (1t10001≤�≤1000) — the number of test cases.

The next 2t2� lines contain the descriptions of the test cases.

The first line of each test case description contains an integer n (2n502≤�≤50) — the length of the array. The second line consists of space-separated elements of the array ai�� (109ai109−109≤��≤109). Elements of array a may repeat and don't have to be unique.

Output

Print t answers to all input test cases.

The first line of the answer of each test case should contain an integer k (0kn0≤�≤�) — the number of actions to sort the array. The next k lines should contain descriptions of the actions formatted as "l r d" (without quotes) where l and r (1l<rn1≤�<�≤�) are the boundaries of the segment being shifted, while d (1drl1≤�≤�−�) 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 d 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 n will be accepted.

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

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

Example
input
Copy
4
2
2 1
3
1 2 1
4
2 4 1 3
5
2 5 1 4 3
output
Copy
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
Note

Explanation of the fourth data set in the example:

  1. The segment a[24]�[2…4] is selected and is shifted to the left by 22[2,5,1,4,3][2,4,5,1,3][2,5,1,4,3]⟶[2,4,5,1,3]
  2. The segment a[15]�[1…5] is then selected and is shifted to the left by 33[2,4,5,1,3][1,3,2,4,5][2,4,5,1,3]⟶[1,3,2,4,5]
  3. After that the segment a[12]�[1…2] is selected and is shifted to the left by 11[1,3,2,4,5][3,1,2,4,5][1,3,2,4,5]⟶[3,1,2,4,5]
  4. And in the end the segment a[13]�[1…3] is selected and is shifted to the left by 11[3,1,2,4,5][1,2,3,4,5][3,1,2,4,5]⟶[1,2,3,4,5]

 



 

Code Examples

#1 Code Example with C++ Programming

Code - C++ Programming

#include<bits/stdc++.h>
using namespace std;


#define ll long long
#define endl '\n'
#define debug(n) cout<<(n)<<endl;
const ll INF = 2e18 + 99;

int main(){
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  int t;
  cin>>t;
  while(t--){
    int n;
    cin>>n;
    int arr[n];
    for(int i = 0; i < n; i++){
      cin>>arr[i];
    }

    int brr[n][3];
    int k = 0;
    for(int i = 1; i < n; i++){
      int key = arr[i];
      if(arr[i] < arr[i-1]){
        int j = i - 1;
        int count = 0;
        while(arr[j] > key && j >= 0){
          count++;
          arr[j + 1] = arr[j];
          j--;
        }
        arr[j + 1] = key;
        brr[k][0] = i + 1;
        brr[k][1] = j + 2;
        brr[k][2] = count;
        k++;
      }

    }
    cout<<k<<endl;
    for(int i = 0; i < k; i++){
      cout<<brr[i][1]<<" "<<brr[i][0]<<" "<<brr[i][2]<<endl;
    }
  }

}
Copy The Code & Try With Live Editor

Input

x
+
cmd
4
2
2 1
3
1 2 1
4
2 4 1 3
5
2 5 1 4 3

Output

x
+
cmd
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
Advertisements

Demonstration


Codeforcess Solution 1579-B B. Shifting Sort ,C++, Java, Js and Python ,1579-B,Codeforcess Solution

Previous
Codeforces solution 1080-B-B. Margarite and the best present codeforces solution
Next
CodeChef solution DETSCORE - Determine the Score CodeChef solution C,C+