Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <algorithm>
- #include <string>
- #include <map>
- #include <set>
- using namespace std;
- int main() {
- ios_base::sync_with_stdio(false);
- cin.tie(0);
- cout.tie(0);
- int n;
- cin >> n;
- vector <int> a(n);
- vector<vector<int> > ans;
- int res = 0;
- for (int i = 0; i < n; i++) cin >> a[i];
- for (int i = 0; i < n; i++) {
- if (a[i] == i+1)
- continue;
- int ind1 = -1;
- for (int j = i; j < n; j++)
- if (a[j] == i+1) {
- ind1 = j;
- break;
- }
- int ind2 = a[i]-1;
- if (ind1 != ind2) {
- vector <int> b = { i + 1, ind1 + 1, ind2 + 1, ind2 + 1, i + 1, ind1 + 1 };
- res++;
- ans.push_back(b);
- swap(a[i], a[ind1]);
- swap(a[ind1], a[ind2]);
- }
- else {
- int k = -1;
- for (int j = 0; j < n; j++)
- if (j != ind1 && j != i) {
- k = j;
- break;
- }
- vector <int> b = { i + 1, ind1 + 1, k + 1, ind1 + 1, i + 1, k + 1 };
- res++;
- ans.push_back(b);
- swap(a[i], a[ind1]);
- }
- }
- cout << res << "\n";
- for (int i = 0; i < (int)ans.size(); i++) {
- for (int j = 0; j < 6; j++)
- cout << ans[i][j] << " ";
- cout << endl;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement