forked from Sam071100/CodeForces-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshiftingSort.cpp
More file actions
40 lines (36 loc) · 829 Bytes
/
shiftingSort.cpp
File metadata and controls
40 lines (36 loc) · 829 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <bits/stdc++.h>
using namespace std;
#define all(x) (x).begin(), (x).end()
void solve()
{
int n;
cin >> n;
vector<int> v(n), copy;
for (int i = 0; i < n; i++)
cin >> v[i];
copy = v;
sort(all(copy));
vector<vector<int>> ans;
for (int i = 0; i < n; i++)
{
if (copy[i] != v[i])
{
auto it = find(v.begin() + i, v.end(), copy[i]);
int x = it - v.begin() - i;
ans.push_back({i + 1, it - v.begin() + 1, x});
rotate(v.begin() + i, v.begin() + x + i, v.begin() + x + i + 1);
}
}
cout << ans.size() << '\n';
for (auto it : ans)
cout << it[0] << ' ' << it[1] << ' ' << it[2] << '\n';
}
int main()
{
int t = 1;
cin >> t;
for (int i = 1; i <= t; i++)
{
solve();
}
}