B题
注意这道题题目要求
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.
只要排序即可,不需要最小化步骤,那就简单了,直接暴力做.从前往后,按顺序,每次排一个数,排的同时输出存储操作步骤,可以用三个数组分别存储操作l,r,d.
举例
- 第一步 2,5,1,4,3 变成 1,2,5,4,3
- 第二步 1,2,5,4,3 变成 1,2,3,5,4
- 第三步 1,2,3,5,4 变成 1,2,3,4,5
以下是一组数据:
输入:
4
2
2 1
3
1 2 1
4
2 4 1 3
5
2 5 1 4 3
输出:
1
1 2 1
1
2 3 1
2
1 3 2
3 4 1
3
1 3 2
3 4 1
3 5 2-------------------------------------------------------分割线
以下是ac代码:
#include <bits/stdc++.h>
using namespace std;
int a[100];
struct node
{
int a, b, c;
} num[100];
int main()
{
// freopen("1.txt", "w", stdout);
int t;
cin >> t;
while (t--)
{
int n;
cin >> n;
int tot = 0;
for (int i = 1; i <= n; i++)
{
cin >> a[i];
for (int j = 1; j < i; j++)
{
if (a[i] < a[j])
{
int x = a[i];
num[tot++] = {j, i, i - j};
for (int k = i; k > j; k--)
a[k] = a[k - 1];
a[j] = x;
break;
}
}
}
cout << tot << "\n";
for (int i = 0; i < tot; i++)
{
printf("%d %d %d\n", num[i].a, num[i].b, num[i].c);
}
/*
for (int i = 1; i <= n; i++)
{
printf("%d ",a[i]);
}
puts("");
*/
}
return 0;
}


京公网安备 11010502036488号