原文链接https://codeforces.com/contest/1579/problem/B
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[1…n]=[a1,a2,…,an]a[1…n]=[a1,a2,…,an].
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:
- You may select arbitrary indices ll and rr (1≤l<r≤n1≤l<r≤n) as the boundaries of the segment.
- Then you replace the segment a[l…r]a[l…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,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…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,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.
Input
The first line contains an integer tt (1≤t≤10001≤t≤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≤n≤50) — the length of the array. The second line consists of space-separated elements of the array aiai (−109≤ai≤109−109≤ai≤109). Elements of array aa may repeat and don't have to be unique.
Output
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≤k≤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≤l<r≤n) are the boundaries of the segment being shifted, while dd (1≤d≤r−l1≤d≤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.
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:
- The segment a[2…4]a[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]
- The segment a[1…5]a[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]
- After that the segment a[1…2]a[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]
- And in the end the segment a[1…3]a[1…3] is selected and is shifted to the left by 11: [3,1,2,4,5]⟶[1,2,3,4,5
------------------------------------------------------------------------------------------------------------------------------
首先,题目要求我们不超过n次操作。那么最坏的情况,我们每次归位一个,这样能不能做到呢,答案是一定的,我们按照大小顺序,先选择最小的一个,找到它在原数组的位置,那么在这个位置和它排序之后的位置之间,我们可以进行题目说的”左移循环“,即把这个数归位。这样,我们花费一次,回归了最小的那个,它已经归位,不会对其他位置的寻找造成影响,以此类推,次小值归位到第二个位置,在2次操作.....一直到剩下两个数,其实最多就n-1次操作,因为最后剩下的两个,只操作一次就够了。
思路对了,关键是细节操作,答案输出比较麻烦,细节见注释
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long int ll;
int a[55],b[55];
int temp[55];
int ans1[55];
int ans2[55];
int ans3[55];
int main()
{
// 1 2 4 3
int t;
cin>>t;
while(t--)
{
int n;
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>a[i];
b[i]=a[i]; //a存排序之后的大小顺序
}
sort(a+1,a+1+n);
int head=1; //从a数组第一个元素开始找
int cnt=0; //几次操作
while(head<=n) //找完全部
{
for(int i=head;i<=n;i++) //每次从当前应该找的位置出发,因为之前都已经排好
{
if(b[i]==a[head]) //找到了
{
if(i==head) //但正好在正确位置
{
head++; //寻找a下一个,结束循环
break;
}
else
{
cnt++; //不在正确位置
ans1[cnt]=head; //存上答案,对正确位置和当前位置进行左移操作
ans2[cnt]=i;
ans3[cnt]=i-head; //移到正确位置的消耗
for(int j=head;j<=i;j++)
{
temp[j]=b[j]; //先存上原始循序
}
for(int j=head;j<=i;j++)
{
if(j+i-head<=i) //修改之后的位置由原始右边位置左移,原始位置在i内
b[j]=temp[j+i-head];
else
b[j]=temp[(j+i-head)%(i)+head-1];//超过i,说明左移越界
}
head++;
}
}
}
}
cout<<cnt<<'\n';
for(int i=1;i<=cnt;i++)
{
cout<<ans1[i]<<" "<<ans2[i]<<" "<<ans3[i]<<'\n';
}
}
return 0;
}