fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main() {
  5. ios::sync_with_stdio(false);
  6. cin.tie(nullptr);
  7. int N;
  8. if(!(cin >> N)) return 0;
  9. vector<int> P(N+1), pos(N+1);
  10. for (int i=1;i<=N;i++){ cin >> P[i]; pos[P[i]]=i; }
  11. vector<pair<int,int>> ops;
  12. for (int i=1;i<=N;i++){
  13. int p = pos[i];
  14. if (p==i) continue;
  15. ops.emplace_back(i,p);
  16. reverse(P.begin()+i, P.begin()+p+1);
  17. for (int k=i;k<=p;k++) pos[P[k]]=k;
  18. }
  19. cout << (int)ops.size() << '\n';
  20. for (auto &op: ops) cout << op.first << ' ' << op.second << '\n';
  21. return 0;
  22. }
Success #stdin #stdout 0.01s 5304KB
stdin
6
1 3 2 5 4 6
stdout
2
2 3
4 5