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
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include <iostream>
  #include <stdio.h>
  using namespace std;


  int nSwaps = 0;
  int swaps[3000][2];

  void swap(int a, int b) {
    swaps[nSwaps][0] = a;
    swaps[nSwaps][1] = b;
    nSwaps++;
  }

  void printSwaps() {
    printf("%d\n", nSwaps * 2);
    for (int i = 0; i < nSwaps; i++) {
      printf("%d ", swaps[i][0]);
    }
    for (int i = nSwaps - 1; i >= 0; i--) {
      printf("%d ", swaps[i][1]);
    }
    printf("\n");
    nSwaps = 0;
  }

  int main() {
    int MAX_HEIGHT = 3000;

    int n;
    cin >> n;
    int initialPositionOfHeight[MAX_HEIGHT + 1];
    for (int i = 1; i <= MAX_HEIGHT; i++) {
      initialPositionOfHeight[i] = -1;
    }

    // read input
    for (int position = 1; position <= n; position++) {
      int height;
      cin >> height;
      initialPositionOfHeight[height] = position;
    }
    // compress heights
    int insert = 1;
    for (int height = 1; height <= MAX_HEIGHT; height++) {
      int position = initialPositionOfHeight[height];
      if (position > 0) {
        initialPositionOfHeight[insert] = position;
        insert++;
      }
    }

    // build graph
    int initial[n + 1];
    for (int height = 1; height <= n; height++) {
      initial[initialPositionOfHeight[height]] = height;
    }

    bool isSorted = true;
    for (int i = 1; i <= n; i++) {
      if (initial[i] != i) {
        isSorted = false;
        break;
      }
    }
    if (isSorted) {
      printf("0");
      return 0;
    }

    bool canSolveInOne = true;
    for (int i = 1; i <= n; i++) {
      if (initial[initial[i]] != i) {
        canSolveInOne = false;
        break;
      }
    }

    if (canSolveInOne) {
      printf("1\n");

      for (int i = 1; i <= n; i++) {
        if (initial[i] > i) {
          swap(i, initial[i]);
        }
      }
      printSwaps();
    } else {
      printf("2\n");

      int middle[n + 1];
      for (int cursor = 1; cursor <= n; cursor++) {
        middle[cursor] = -1;
      }

      for (int cursor = 1; cursor <= n; cursor++) {
        if (initial[cursor] == cursor) {
          middle[cursor] = cursor;
        } else if (middle[cursor] != -1) {

        } else {
          int position = cursor;
          int value = initial[cursor];
          while (true) {
            middle[position] = value;
            middle[value] = position;
            if (initial[value] == position) {
              break;
            }
            position = initialPositionOfHeight[position];
            value = initial[value];
          }
        }
      }

      int lookup[n + 1];
      for (int i = 1; i <= n; i++) {
        lookup[middle[i]] = i;
      }

      for (int i = 1; i <= n; i++) {
        if (initial[i] > middle[i]) {
          swap(i, lookup[initial[i]]);
        }
      }
      printSwaps();

      for (int i = 1; i <= n; i++) {
        if (middle[i] > i) {
          swap(i, middle[i]);
        }
      }
      printSwaps();
    }

    return 0;
  }