#include <iostream> #include <map> #include <set> #include <vector> #include <bitset> using namespace std; int t[3005], spojna[3005], rep[3005], it, a, b, wszystkie, indeks[3005]; set <int> s; map<int, int> m; int v[3005], wstecz[3005]; vector<pair<int, int> > zap[3]; bool zrobione[3005]; bitset<3005> zamienione, odwiedzone; int main() { int n; cin >> n; wszystkie = n; for(int i = 1; i <= n; ++i) { cin >> t[i]; s.insert(t[i]); } int nowy_numer = 1; for(int i : s) { m[i] = nowy_numer; nowy_numer++; } for(int i = 1; i <= n; ++i) { t[i] = m[t[i]]; v[i] = t[i]; wstecz[t[i]] = i; } int a, b; for(int i = 1; i <= n; ++i) { int x = i; if(odwiedzone[x]) continue; while(true) { a = v[x]; b = wstecz[x]; if(a == b) break; odwiedzone[a] = 1; odwiedzone[b] = 1; zap[0].push_back({a, b}); wstecz[v[a]] = b; swap(v[a], v[b]); wstecz[x] = a; x = b; } } for(int i = 1; i <= n; ++i) { if(v[i] > i ) zap[1].push_back({i, v[i]}); } if(zap[0].empty() && zap[1].empty()) cout << 0 << "\n"; else if(zap[0].empty() && !zap[1].empty()) { cout << 1 << "\n"; cout << (int)zap[1].size() * 2 << "\n"; for(int i = 0; i <(int)zap[1].size(); ++i) cout << zap[1][i].first << " "; for(int i = (int)zap[1].size() - 1; ~i; --i) cout << zap[1][i].second << " "; } else if(!zap[0].empty() && zap[1].empty()) { cout << 1 << "\n"; cout << (int)zap[0].size() * 2 << "\n"; for(int i = 0; i <(int)zap[0].size(); ++i) cout << zap[0][i].first << " "; for(int i = (int)zap[0].size() - 1; ~i; --i) cout << zap[0][i].second << " "; } else { cout << 2 << "\n"; cout << (int)zap[0].size() * 2 << "\n"; for(int i = 0; i <(int)zap[0].size(); ++i) cout << zap[0][i].first << " "; for(int i = (int)zap[0].size() - 1; ~i; --i) cout << zap[0][i].second << " "; cout << "\n" << (int)zap[1].size() * 2 << "\n"; for(int i = 0; i <(int)zap[1].size(); ++i) cout << zap[1][i].first << " "; for(int i = (int)zap[1].size() - 1; ~i; --i) cout << zap[1][i].second << " "; } 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 | #include <iostream> #include <map> #include <set> #include <vector> #include <bitset> using namespace std; int t[3005], spojna[3005], rep[3005], it, a, b, wszystkie, indeks[3005]; set <int> s; map<int, int> m; int v[3005], wstecz[3005]; vector<pair<int, int> > zap[3]; bool zrobione[3005]; bitset<3005> zamienione, odwiedzone; int main() { int n; cin >> n; wszystkie = n; for(int i = 1; i <= n; ++i) { cin >> t[i]; s.insert(t[i]); } int nowy_numer = 1; for(int i : s) { m[i] = nowy_numer; nowy_numer++; } for(int i = 1; i <= n; ++i) { t[i] = m[t[i]]; v[i] = t[i]; wstecz[t[i]] = i; } int a, b; for(int i = 1; i <= n; ++i) { int x = i; if(odwiedzone[x]) continue; while(true) { a = v[x]; b = wstecz[x]; if(a == b) break; odwiedzone[a] = 1; odwiedzone[b] = 1; zap[0].push_back({a, b}); wstecz[v[a]] = b; swap(v[a], v[b]); wstecz[x] = a; x = b; } } for(int i = 1; i <= n; ++i) { if(v[i] > i ) zap[1].push_back({i, v[i]}); } if(zap[0].empty() && zap[1].empty()) cout << 0 << "\n"; else if(zap[0].empty() && !zap[1].empty()) { cout << 1 << "\n"; cout << (int)zap[1].size() * 2 << "\n"; for(int i = 0; i <(int)zap[1].size(); ++i) cout << zap[1][i].first << " "; for(int i = (int)zap[1].size() - 1; ~i; --i) cout << zap[1][i].second << " "; } else if(!zap[0].empty() && zap[1].empty()) { cout << 1 << "\n"; cout << (int)zap[0].size() * 2 << "\n"; for(int i = 0; i <(int)zap[0].size(); ++i) cout << zap[0][i].first << " "; for(int i = (int)zap[0].size() - 1; ~i; --i) cout << zap[0][i].second << " "; } else { cout << 2 << "\n"; cout << (int)zap[0].size() * 2 << "\n"; for(int i = 0; i <(int)zap[0].size(); ++i) cout << zap[0][i].first << " "; for(int i = (int)zap[0].size() - 1; ~i; --i) cout << zap[0][i].second << " "; cout << "\n" << (int)zap[1].size() * 2 << "\n"; for(int i = 0; i <(int)zap[1].size(); ++i) cout << zap[1][i].first << " "; for(int i = (int)zap[1].size() - 1; ~i; --i) cout << zap[1][i].second << " "; } return 0; } |