Advertisement
OIQ

Untitled

OIQ
Mar 15th, 2020
295
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.14 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <string>
  5. #include <map>
  6. #include <set>
  7. using namespace std;
  8.  
  9. int main() {
  10.     ios_base::sync_with_stdio(false);
  11.     cin.tie(0);
  12.     cout.tie(0);
  13.     int n;
  14.     cin >> n;
  15.     vector <int> a(n);
  16.     vector<vector<int> > ans;
  17.     int res = 0;
  18.     for (int i = 0; i < n; i++) cin >> a[i];
  19.  
  20.     for (int i = 0; i < n; i++) {
  21.         if (a[i] == i+1)
  22.             continue;
  23.         int ind1 = -1;
  24.         for (int j = i; j < n; j++)
  25.             if (a[j] == i+1) {
  26.                 ind1 = j;
  27.                 break;
  28.             }
  29.         int ind2 = a[i]-1;
  30.         if (ind1 != ind2) {
  31.             vector <int> b = { i + 1, ind1 + 1, ind2 + 1, ind2 + 1, i + 1, ind1 + 1 };
  32.             res++;
  33.             ans.push_back(b);
  34.             swap(a[i], a[ind1]);
  35.             swap(a[ind1], a[ind2]);
  36.         }
  37.         else {
  38.             int k = -1;
  39.             for (int j = 0; j < n; j++)
  40.                 if (j != ind1 && j != i) {
  41.                     k = j;
  42.                     break;
  43.                 }
  44.             vector <int> b = { i + 1, ind1 + 1, k + 1, ind1 + 1, i + 1, k + 1 };
  45.             res++;
  46.             ans.push_back(b);
  47.             swap(a[i], a[ind1]);
  48.         }
  49.  
  50.     }
  51.     cout << res << "\n";
  52.     for (int i = 0; i < (int)ans.size(); i++) {
  53.         for (int j = 0; j < 6; j++)
  54.             cout << ans[i][j] << " ";
  55.         cout << endl;
  56.     }
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement