-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path1579B_Shifting Sort.cpp
75 lines (63 loc) · 1.16 KB
/
1579B_Shifting Sort.cpp
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <iostream>
#include <vector>
#include <algorithm>
#include <deque>
#define INF 987654321
using namespace std;
struct Info {
int l, r, d;
Info(int l, int r, int d) : l(l), r(r), d(d) { }
};
int n;
vector<int> a;
vector<Info> infos;
int findMin(int start, int end)
{
int minIdx = start;
for (int i = start; i < end; i++) {
if (a[i] < a[minIdx]) {
minIdx = i;
}
}
return minIdx;
}
void shiftingSort(int start, int end)
{
deque<int> dq;
for (int i = start; i <= end; i++) {
dq.push_back(a[i]);
}
for (int i = 0; i < end - start; i++) {
dq.push_back(dq.front());
dq.pop_front();
}
for (int i = 0; i < dq.size(); i++) {
a[start + i] = dq[i];
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int T;
cin >> T;
while (T--) {
cin >> n;
a.resize(n);
for (int i = 0; i < n; i++) cin >> a[i];
for (int i = 0; i < n; i++) {
int idx = findMin(i, n);
if (idx > i) {
shiftingSort(i, idx);
infos.push_back(Info(i + 1, idx + 1, idx - i));
}
}
cout << infos.size() << "\n";
for (auto &info : infos) {
cout << info.l << " " << info.r << " " << info.d << "\n";
}
infos.clear();
}
return 0;
}