#include<cstdio> #include<algorithm> #include<vector> std::vector<std::pair<int, int> > results[3010][2]; int H[3010]; int S[3010]; int P[3010]; bool used[3010]; int main() { int N; scanf("%d", &N); for (int i=1; i<=N; ++i) { int v; scanf("%d", &v); S[i] = H[i] = v; used[i] = false; } std::sort(S+1, S+N+1); for (int i=1; i<=N; ++i) { P[S[i]] = i; } results[2][0].push_back({1,2}); results[3][0].push_back({2,3}); results[3][1].push_back({1,2}); results[4][0].push_back({2,4}); results[4][1].push_back({1,2}); results[4][1].push_back({3,4}); results[5][0].push_back({2,5}); results[5][0].push_back({3,4}); results[5][1].push_back({1,2}); results[5][1].push_back({3,5}); results[6][0].push_back({2,6}); results[6][0].push_back({3,5}); results[6][1].push_back({1,2}); results[6][1].push_back({4,5}); results[6][1].push_back({3,6}); for (int i=7; i<=N; ++i) { int lsize = i/2; int rsize = i-lsize; if (results[lsize][0].size()*2 + results[rsize][0].size()*2 == i-2) { lsize--; rsize++; } results[i][0].push_back({1,lsize+1}); //printf("%d %d %d %d\n", i,0,1,lsize+1); for (int k=0; k<=1; k++) { for (const auto &e : results[lsize][k]) { int nl = ((e.first==1)?(lsize+1):(e.first)); int nr = ((e.second==1)?(lsize+1):(e.second)); results[i][k].push_back({nl, nr}); //printf("+%d %d %d %d\n", i,k,nl,nr); } for (const auto &e : results[rsize][k]) { int nl = ((e.first==1)?1:(e.first+lsize)); int nr = ((e.second==1)?1:(e.second+lsize)); results[i][k].push_back({nl, nr}); //printf("-%d %d %d %d\n", i,k,nl,nr); } } //remap std::vector<bool> used(i, false); for (const auto &e : results[i][0]) { used[e.first] = true; used[e.second] = true; } int empty = -1; for (int j=1; j<=i; ++j) { if (used[j] == false) { empty = j; } } for (int k=0; k<=1; k++) { for (auto &e : results[i][k]) { e.first -= (empty-1); e.second -= (empty-1); if (e.first < 1) { e.first += i; } if (e.second < 1) { e.second += i; } } } } std::vector<std::pair<int, int> > out[2]; for (int i=1; i<=N; ++i) { std::vector<int> positions; int v = i; while (used[v]==false) { positions.push_back(v); used[v] = true; v = P[H[v]]; } int len = positions.size(); for (int k=0; k<=1; k++) { for (const auto &e : results[len][k]) { out[k].push_back({positions[e.first-1], positions[e.second-1]}); } } } printf("%d\n", (out[0].empty()?0:(out[1].empty()?1:2))); for (int i=0; i<=1; ++i) { if (!out[i].empty()) { printf("%d\n", 2*(int)out[i].size()); for (int j=0; j<out[i].size(); ++j) { printf("%d ", out[i][j].first); } for (int j=out[i].size()-1; j>=0; --j) { printf("%d ", out[i][j].second); } printf("\n"); } } 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 | #include<cstdio> #include<algorithm> #include<vector> std::vector<std::pair<int, int> > results[3010][2]; int H[3010]; int S[3010]; int P[3010]; bool used[3010]; int main() { int N; scanf("%d", &N); for (int i=1; i<=N; ++i) { int v; scanf("%d", &v); S[i] = H[i] = v; used[i] = false; } std::sort(S+1, S+N+1); for (int i=1; i<=N; ++i) { P[S[i]] = i; } results[2][0].push_back({1,2}); results[3][0].push_back({2,3}); results[3][1].push_back({1,2}); results[4][0].push_back({2,4}); results[4][1].push_back({1,2}); results[4][1].push_back({3,4}); results[5][0].push_back({2,5}); results[5][0].push_back({3,4}); results[5][1].push_back({1,2}); results[5][1].push_back({3,5}); results[6][0].push_back({2,6}); results[6][0].push_back({3,5}); results[6][1].push_back({1,2}); results[6][1].push_back({4,5}); results[6][1].push_back({3,6}); for (int i=7; i<=N; ++i) { int lsize = i/2; int rsize = i-lsize; if (results[lsize][0].size()*2 + results[rsize][0].size()*2 == i-2) { lsize--; rsize++; } results[i][0].push_back({1,lsize+1}); //printf("%d %d %d %d\n", i,0,1,lsize+1); for (int k=0; k<=1; k++) { for (const auto &e : results[lsize][k]) { int nl = ((e.first==1)?(lsize+1):(e.first)); int nr = ((e.second==1)?(lsize+1):(e.second)); results[i][k].push_back({nl, nr}); //printf("+%d %d %d %d\n", i,k,nl,nr); } for (const auto &e : results[rsize][k]) { int nl = ((e.first==1)?1:(e.first+lsize)); int nr = ((e.second==1)?1:(e.second+lsize)); results[i][k].push_back({nl, nr}); //printf("-%d %d %d %d\n", i,k,nl,nr); } } //remap std::vector<bool> used(i, false); for (const auto &e : results[i][0]) { used[e.first] = true; used[e.second] = true; } int empty = -1; for (int j=1; j<=i; ++j) { if (used[j] == false) { empty = j; } } for (int k=0; k<=1; k++) { for (auto &e : results[i][k]) { e.first -= (empty-1); e.second -= (empty-1); if (e.first < 1) { e.first += i; } if (e.second < 1) { e.second += i; } } } } std::vector<std::pair<int, int> > out[2]; for (int i=1; i<=N; ++i) { std::vector<int> positions; int v = i; while (used[v]==false) { positions.push_back(v); used[v] = true; v = P[H[v]]; } int len = positions.size(); for (int k=0; k<=1; k++) { for (const auto &e : results[len][k]) { out[k].push_back({positions[e.first-1], positions[e.second-1]}); } } } printf("%d\n", (out[0].empty()?0:(out[1].empty()?1:2))); for (int i=0; i<=1; ++i) { if (!out[i].empty()) { printf("%d\n", 2*(int)out[i].size()); for (int j=0; j<out[i].size(); ++j) { printf("%d ", out[i][j].first); } for (int j=out[i].size()-1; j>=0; --j) { printf("%d ", out[i][j].second); } printf("\n"); } } return 0; } |