// Online C++ Compiler - Build, Compile and Run your C++ programs online in your favorite browser #include<iostream> #include<vector> #include<queue> #include<algorithm> using namespace std; void testcase() { int n; cin >> n; vector <int> A(n), AS(n); for (int i = 0; i < n; i++) { cin >> A[i]; AS[i] = A[i]; } sort(AS.begin(), AS.end()); for (int i = 0; i < n; i++) { A[i] = lower_bound(AS.begin(), AS.end(), A[i]) - AS.begin(); } vector < deque <int> > Ans; vector <int> Last_it(n, -1); for (int it = 0; ; it++) { deque <int> Cur_row; for (int i = 0; i < n; i++) { int ai = A[i]; if (A[i] == i || Last_it[i] == it || Last_it[ai] == it) { continue; } Last_it[i] = it; Last_it[ai] = it; swap(A[i], A[ai]); Cur_row.push_front(i); Cur_row.push_back(ai); } if (!Cur_row.empty()) { Ans.push_back(Cur_row); } else { break; } } cout << Ans.size() <<endl; for (auto& dq : Ans) { cout << dq.size() <<endl; for (int v : dq) { cout << v+1 << " "; } cout <<endl; } } int main() { testcase(); return 0; }
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 | // Online C++ Compiler - Build, Compile and Run your C++ programs online in your favorite browser #include<iostream> #include<vector> #include<queue> #include<algorithm> using namespace std; void testcase() { int n; cin >> n; vector <int> A(n), AS(n); for (int i = 0; i < n; i++) { cin >> A[i]; AS[i] = A[i]; } sort(AS.begin(), AS.end()); for (int i = 0; i < n; i++) { A[i] = lower_bound(AS.begin(), AS.end(), A[i]) - AS.begin(); } vector < deque <int> > Ans; vector <int> Last_it(n, -1); for (int it = 0; ; it++) { deque <int> Cur_row; for (int i = 0; i < n; i++) { int ai = A[i]; if (A[i] == i || Last_it[i] == it || Last_it[ai] == it) { continue; } Last_it[i] = it; Last_it[ai] = it; swap(A[i], A[ai]); Cur_row.push_front(i); Cur_row.push_back(ai); } if (!Cur_row.empty()) { Ans.push_back(Cur_row); } else { break; } } cout << Ans.size() <<endl; for (auto& dq : Ans) { cout << dq.size() <<endl; for (int v : dq) { cout << v+1 << " "; } cout <<endl; } } int main() { testcase(); return 0; } |