#include <iostream> using namespace std; void swap(int arr[], int pos1, int pos2) { int temp; temp = arr[pos1]; arr[pos1] = arr[pos2]; arr[pos2] = temp; } int partition(int arr[], int low, int high, int pivot) { int i = low; int j = low; while (i <= high) { if (arr[i] > pivot) { i++; } else { swap(arr, i, j); i++; j++; } } return j - 1; } void quickSort(int arr[], int low, int high) { if (low < high) { int pivot = arr[high]; int pos = partition(arr, low, high, pivot); quickSort(arr, low, pos - 1); quickSort(arr, pos + 1, high); } } void doMap(int arr1[], int arr2[], int mapArr[], int n) { for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (arr1[i] == arr2[j]) { mapArr[i] = j; break; } } } } int main() { int n; cin >> n; int aHight[n], sortHight[n], endMap[n], chMap[n / 2 + 1][n + 1], listC[n + 1]; bool wasMap[n]; for (int i = 0; i < n; i++) { cin >> aHight[i]; sortHight[i] = aHight[i]; } quickSort(sortHight, 0, n - 1); int maxC = 0, max = 1; int cCount = 1, j, lCount, allChanges = 0; bool first = true; while (cCount > 0) { cCount = 0; allChanges = 0; for (int i = 0; i < n; i++) { wasMap[i] = false; } doMap(aHight, sortHight, endMap, n); for (int i = 0; i < n; i++) { if ((endMap[i] != i) && (wasMap[i] == false)) { wasMap[i] = true; cCount++; j = i; lCount = 1; chMap[cCount][lCount] = i; while (endMap[j] != i) { j = endMap[j]; chMap[cCount][++lCount] = j; wasMap[j] = true; } chMap[cCount][0] = lCount; allChanges += (lCount / 2) * 2; } } chMap[0][0] = cCount; listC[0] = allChanges; max = 0; for (int i = 1; i <= chMap[0][0]; i++) { if (chMap[i][0] > max) max = chMap[i][0]; } if (max == 2) maxC = 1; else if (max > 2) maxC = 2; if (first){ cout << maxC; first = false; } int pos = 1; for (int i = 1; i <= chMap[0][0]; i++) { for (int j = 1; j <= (chMap[i][0] / 2); j++) { listC[pos] = chMap[i][j] + 1; listC[listC[0] - pos + 1] = chMap[i][chMap[i][0]-j+1] + 1; swap(aHight, chMap[i][j], chMap[i][chMap[i][0]-j+1]); pos++; } } if (listC[0]>0) cout << endl << listC[0] << endl; for (int i = 1; i <= listC[0]; i++) { cout << listC[i] << " "; } } 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 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 138 | #include <iostream> using namespace std; void swap(int arr[], int pos1, int pos2) { int temp; temp = arr[pos1]; arr[pos1] = arr[pos2]; arr[pos2] = temp; } int partition(int arr[], int low, int high, int pivot) { int i = low; int j = low; while (i <= high) { if (arr[i] > pivot) { i++; } else { swap(arr, i, j); i++; j++; } } return j - 1; } void quickSort(int arr[], int low, int high) { if (low < high) { int pivot = arr[high]; int pos = partition(arr, low, high, pivot); quickSort(arr, low, pos - 1); quickSort(arr, pos + 1, high); } } void doMap(int arr1[], int arr2[], int mapArr[], int n) { for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (arr1[i] == arr2[j]) { mapArr[i] = j; break; } } } } int main() { int n; cin >> n; int aHight[n], sortHight[n], endMap[n], chMap[n / 2 + 1][n + 1], listC[n + 1]; bool wasMap[n]; for (int i = 0; i < n; i++) { cin >> aHight[i]; sortHight[i] = aHight[i]; } quickSort(sortHight, 0, n - 1); int maxC = 0, max = 1; int cCount = 1, j, lCount, allChanges = 0; bool first = true; while (cCount > 0) { cCount = 0; allChanges = 0; for (int i = 0; i < n; i++) { wasMap[i] = false; } doMap(aHight, sortHight, endMap, n); for (int i = 0; i < n; i++) { if ((endMap[i] != i) && (wasMap[i] == false)) { wasMap[i] = true; cCount++; j = i; lCount = 1; chMap[cCount][lCount] = i; while (endMap[j] != i) { j = endMap[j]; chMap[cCount][++lCount] = j; wasMap[j] = true; } chMap[cCount][0] = lCount; allChanges += (lCount / 2) * 2; } } chMap[0][0] = cCount; listC[0] = allChanges; max = 0; for (int i = 1; i <= chMap[0][0]; i++) { if (chMap[i][0] > max) max = chMap[i][0]; } if (max == 2) maxC = 1; else if (max > 2) maxC = 2; if (first){ cout << maxC; first = false; } int pos = 1; for (int i = 1; i <= chMap[0][0]; i++) { for (int j = 1; j <= (chMap[i][0] / 2); j++) { listC[pos] = chMap[i][j] + 1; listC[listC[0] - pos + 1] = chMap[i][chMap[i][0]-j+1] + 1; swap(aHight, chMap[i][j], chMap[i][chMap[i][0]-j+1]); pos++; } } if (listC[0]>0) cout << endl << listC[0] << endl; for (int i = 1; i <= listC[0]; i++) { cout << listC[i] << " "; } } return 0; } |