#include <cstdio> #include <algorithm> #include <vector> #include <algorithm> #include <cassert> #include <cstdlib> #define MAX 3009 #define DB if(0) #define PR if(0) #define RND if (0) int v[MAX], valToPos[MAX], m[MAX]; void solve(int n, std::vector<int> &r) { std::vector<int> in, out; int used[n], w[n]; for (int i=0; i<n; i++) { used[i] = 0; w[v[i]] = i; } DB printf("v:\n"); DB for(int i=0; i<n; i++) printf("%d ", v[i]); DB printf("\nwhere:\n"); DB for(int i=0; i<n; i++) printf("%d ", w[i]); DB printf("\n"); for (int j=0; j<n; j++) { int valueToPush = j; int whereToPush = j; while (valueToPush != v[whereToPush] && !used[valueToPush] && !used[v[whereToPush]]) { DB printf("valueToPush: %d, whereToPush: %d, v[whereToPush] = %d, w[valueToPush] = %d\n", valueToPush, whereToPush, v[whereToPush], w[valueToPush]); DB printf("swapping positions: %d and %d\n", w[valueToPush], whereToPush); in.push_back(w[valueToPush]); out.push_back(whereToPush); used[v[whereToPush]] = true; used[valueToPush] = true; valueToPush = w[valueToPush]; whereToPush = v[whereToPush]; DB printf("new values value = %d, where = %d\n", valueToPush, whereToPush); } } int cnt = (int)in.size(); for (int i=0; i<cnt; i++) { r.push_back(in[i]); std::swap(v[in[i]], v[out[i]]); } for (int i=cnt-1; i>=0; i--) { r.push_back(out[i]); } } int main() { // int sd = 1231; // srand(sd); // while(true) { // for(int i=0; i<MAX; i++) { // v[i] = valToPos[i] = m[i] = 0; // } int n;// = rand() % 300 + 1; // if (n % 2) n++; // printf("%d\n", n); scanf("%d\n", &n); // for (int i=0; i<n; i++) v[i] = 1 * (i+1);// + rand() % 10; // std::random_shuffle(v, v + n); for (int i = 0; i < n; i++) { // printf("%d ", v[i]); scanf("%d", v + i); valToPos[v[i]] = i + 1; } // printf("\n"); int cnt = 0; for (int i = 0; i < MAX; i++) { if (valToPos[i]) m[valToPos[i] - 1] = cnt++; } for (int i=0; i<n; i++) v[i] = m[i]; std::vector<int> r1, r2, r3; solve(n, r1); solve(n, r2); // solve(n, r3); if (r3.size()) printf("3\n"); else if (r2.size()) printf("2\n"); else if (r1.size()) printf("1\n"); else printf("0\n"); if (r1.size()) { printf("%zu\n", r1.size()); for (int i = 0; i < r1.size(); i++) { printf("%d ", r1[i] + 1); } printf("\n"); } if (r2.size()) { printf("%zu\n", r2.size()); for (int i = 0; i < r2.size(); i++) { printf("%d ", r2[i] + 1); } printf("\n"); } // if (r3.size()) { // printf("%zu\n", r3.size()); // for (int i = 0; i < r3.size(); i++) { // printf("%d ", r3[i] + 1); // } // printf("\n"); // } // for (int i = 0; i < n - 1; i++) { // assert (v[i] < v[i+1]); // } PR printf("\n-------\n"); // } }
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 | #include <cstdio> #include <algorithm> #include <vector> #include <algorithm> #include <cassert> #include <cstdlib> #define MAX 3009 #define DB if(0) #define PR if(0) #define RND if (0) int v[MAX], valToPos[MAX], m[MAX]; void solve(int n, std::vector<int> &r) { std::vector<int> in, out; int used[n], w[n]; for (int i=0; i<n; i++) { used[i] = 0; w[v[i]] = i; } DB printf("v:\n"); DB for(int i=0; i<n; i++) printf("%d ", v[i]); DB printf("\nwhere:\n"); DB for(int i=0; i<n; i++) printf("%d ", w[i]); DB printf("\n"); for (int j=0; j<n; j++) { int valueToPush = j; int whereToPush = j; while (valueToPush != v[whereToPush] && !used[valueToPush] && !used[v[whereToPush]]) { DB printf("valueToPush: %d, whereToPush: %d, v[whereToPush] = %d, w[valueToPush] = %d\n", valueToPush, whereToPush, v[whereToPush], w[valueToPush]); DB printf("swapping positions: %d and %d\n", w[valueToPush], whereToPush); in.push_back(w[valueToPush]); out.push_back(whereToPush); used[v[whereToPush]] = true; used[valueToPush] = true; valueToPush = w[valueToPush]; whereToPush = v[whereToPush]; DB printf("new values value = %d, where = %d\n", valueToPush, whereToPush); } } int cnt = (int)in.size(); for (int i=0; i<cnt; i++) { r.push_back(in[i]); std::swap(v[in[i]], v[out[i]]); } for (int i=cnt-1; i>=0; i--) { r.push_back(out[i]); } } int main() { // int sd = 1231; // srand(sd); // while(true) { // for(int i=0; i<MAX; i++) { // v[i] = valToPos[i] = m[i] = 0; // } int n;// = rand() % 300 + 1; // if (n % 2) n++; // printf("%d\n", n); scanf("%d\n", &n); // for (int i=0; i<n; i++) v[i] = 1 * (i+1);// + rand() % 10; // std::random_shuffle(v, v + n); for (int i = 0; i < n; i++) { // printf("%d ", v[i]); scanf("%d", v + i); valToPos[v[i]] = i + 1; } // printf("\n"); int cnt = 0; for (int i = 0; i < MAX; i++) { if (valToPos[i]) m[valToPos[i] - 1] = cnt++; } for (int i=0; i<n; i++) v[i] = m[i]; std::vector<int> r1, r2, r3; solve(n, r1); solve(n, r2); // solve(n, r3); if (r3.size()) printf("3\n"); else if (r2.size()) printf("2\n"); else if (r1.size()) printf("1\n"); else printf("0\n"); if (r1.size()) { printf("%zu\n", r1.size()); for (int i = 0; i < r1.size(); i++) { printf("%d ", r1[i] + 1); } printf("\n"); } if (r2.size()) { printf("%zu\n", r2.size()); for (int i = 0; i < r2.size(); i++) { printf("%d ", r2[i] + 1); } printf("\n"); } // if (r3.size()) { // printf("%zu\n", r3.size()); // for (int i = 0; i < r3.size(); i++) { // printf("%d ", r3[i] + 1); // } // printf("\n"); // } // for (int i = 0; i < n - 1; i++) { // assert (v[i] < v[i+1]); // } PR printf("\n-------\n"); // } } |