#include <stdio.h> #include <time.h> #include <math.h> #include <stdlib.h> #include <string.h> #include <map> #include <string> #include <vector> #include <iostream> #include <sstream> #include <queue> #include <algorithm> #include <assert.h> using namespace std; #define ll long long #define PB push_back #define FOR(a,start,end) for(int a=int(start); a<int(end); a++) #define INF INT_MAX #define SORT(a) sort(a.begin(),a.end()) #define CL(a,x) memset(a,x,sizeof(a)) #define REP(a,x) for(int a=0;a<x;a++) #define REP1(a,x) for(int a=1;a<=x;a++) #define MP make_pair typedef vector<ll> vi; typedef pair<ll, ll> pii; typedef vector<string> vs; typedef vector<pii> vii; typedef vector<vector<ll> > vvi; typedef vector<vector<string> > vvs; typedef vector<pair<string, string> > vss; typedef pair<string, string> pss; typedef pair<ll, pii> ppii; typedef vector<ppii> vppii; typedef vector<vector<pii> > vvii; typedef vector<vvi> vvvi; ll n, k, q, d, r, g, b, z, p, a, c; char zn; string wa; vi vk, va, vk1, vk2; vii vkx; vvi vvk; vector<bool> vb; bool ok() { for (int i = 0; i < n; i++) if (i!=vkx[i].second) return false; return true; } int bs(ll a) { int i1 = 0; int i2 = n - 1; int i0 = (i1 + i2) / 2; while (i2 - i1 > 1) { if (vkx[i0].first < a) i1 = i0; else if (vkx[i0].first > a) i2 = i0; else return i0; i0 = (i1 + i2) / 2; } if (vkx[i1].first == a) return i1; return i2; } vii genp() { vii vpx; vpx.clear(); vb.assign(n, false); for (int i = 0; i < n; i++) { if (vb[i]) continue; if (vkx[i].second == i) { vb[i] = true; } else { if (vb[vkx[i].second]) continue; if (vkx[vkx[i].second].second == i) { vpx.push_back(MP(i, vkx[i].second)); vb[i] = true; vb[vkx[i].second] = true; } } } for (int i = 0; i < n; i++) { if (!vb[i]) { if (vb[vkx[i].second]) continue; int a = i; int b = vkx[i].second; while (a >= 0) { vpx.push_back(MP(a, b)); vb[a] = true; vb[b] = true; a = bs(vk[a]); b = vkx[b].second; if (vb[a]) a = -1; if (vb[b]) a = -1; if (a == b) a = -1; } } } return vpx; } int main() { //freopen("c:\\wojtek\\uva\\pa\\debug\\tes.in", "rt", stdin); //d = 1000000007; //while (1) { cin >> n; vkx.assign(n, MP(0,0)); vk.assign(n, 0); for (int i = 0; i < n; i++) { cin >> vkx[i].first; vk[i] = vkx[i].first; vkx[i].second = i; } SORT(vkx); z = 0; vvk.clear(); while (!ok()) { vii vpx = genp(); va.clear(); for (int i = 0; i < (int)vpx.size(); i++) { va.push_back(vpx[i].first); } for (int i = (int)vpx.size()-1; i >=0; i--) { va.push_back(vpx[i].second); } vvk.push_back(va); for (int i = 0; i < (int)vpx.size(); i++) { int a = vk[vpx[i].first]; vk[vpx[i].first] = vk[vpx[i].second]; vk[vpx[i].second] = a; } for (int i = 0; i < n; i++) { vkx[i] = MP(vk[i], i); } SORT(vkx); } cout << (int)vvk.size() << "\n"; for (int i = 0; i < (int)vvk.size(); i++) { cout << (int)vvk[i].size() << "\n"; for (int j = 0; j < (int)vvk[i].size(); j++) { cout << vvk[i][j] + 1 << " "; } cout << "\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 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 | #include <stdio.h> #include <time.h> #include <math.h> #include <stdlib.h> #include <string.h> #include <map> #include <string> #include <vector> #include <iostream> #include <sstream> #include <queue> #include <algorithm> #include <assert.h> using namespace std; #define ll long long #define PB push_back #define FOR(a,start,end) for(int a=int(start); a<int(end); a++) #define INF INT_MAX #define SORT(a) sort(a.begin(),a.end()) #define CL(a,x) memset(a,x,sizeof(a)) #define REP(a,x) for(int a=0;a<x;a++) #define REP1(a,x) for(int a=1;a<=x;a++) #define MP make_pair typedef vector<ll> vi; typedef pair<ll, ll> pii; typedef vector<string> vs; typedef vector<pii> vii; typedef vector<vector<ll> > vvi; typedef vector<vector<string> > vvs; typedef vector<pair<string, string> > vss; typedef pair<string, string> pss; typedef pair<ll, pii> ppii; typedef vector<ppii> vppii; typedef vector<vector<pii> > vvii; typedef vector<vvi> vvvi; ll n, k, q, d, r, g, b, z, p, a, c; char zn; string wa; vi vk, va, vk1, vk2; vii vkx; vvi vvk; vector<bool> vb; bool ok() { for (int i = 0; i < n; i++) if (i!=vkx[i].second) return false; return true; } int bs(ll a) { int i1 = 0; int i2 = n - 1; int i0 = (i1 + i2) / 2; while (i2 - i1 > 1) { if (vkx[i0].first < a) i1 = i0; else if (vkx[i0].first > a) i2 = i0; else return i0; i0 = (i1 + i2) / 2; } if (vkx[i1].first == a) return i1; return i2; } vii genp() { vii vpx; vpx.clear(); vb.assign(n, false); for (int i = 0; i < n; i++) { if (vb[i]) continue; if (vkx[i].second == i) { vb[i] = true; } else { if (vb[vkx[i].second]) continue; if (vkx[vkx[i].second].second == i) { vpx.push_back(MP(i, vkx[i].second)); vb[i] = true; vb[vkx[i].second] = true; } } } for (int i = 0; i < n; i++) { if (!vb[i]) { if (vb[vkx[i].second]) continue; int a = i; int b = vkx[i].second; while (a >= 0) { vpx.push_back(MP(a, b)); vb[a] = true; vb[b] = true; a = bs(vk[a]); b = vkx[b].second; if (vb[a]) a = -1; if (vb[b]) a = -1; if (a == b) a = -1; } } } return vpx; } int main() { //freopen("c:\\wojtek\\uva\\pa\\debug\\tes.in", "rt", stdin); //d = 1000000007; //while (1) { cin >> n; vkx.assign(n, MP(0,0)); vk.assign(n, 0); for (int i = 0; i < n; i++) { cin >> vkx[i].first; vk[i] = vkx[i].first; vkx[i].second = i; } SORT(vkx); z = 0; vvk.clear(); while (!ok()) { vii vpx = genp(); va.clear(); for (int i = 0; i < (int)vpx.size(); i++) { va.push_back(vpx[i].first); } for (int i = (int)vpx.size()-1; i >=0; i--) { va.push_back(vpx[i].second); } vvk.push_back(va); for (int i = 0; i < (int)vpx.size(); i++) { int a = vk[vpx[i].first]; vk[vpx[i].first] = vk[vpx[i].second]; vk[vpx[i].second] = a; } for (int i = 0; i < n; i++) { vkx[i] = MP(vk[i], i); } SORT(vkx); } cout << (int)vvk.size() << "\n"; for (int i = 0; i < (int)vvk.size(); i++) { cout << (int)vvk[i].size() << "\n"; for (int j = 0; j < (int)vvk[i].size(); j++) { cout << vvk[i][j] + 1 << " "; } cout << "\n"; } // } return 0; } |