#include <iostream> #include <vector> #include <algorithm> using namespace std; #define print(g1,g2) \ cout << g1.size() + g2.size() << "\n"; \ for(int i = 0; i < g1.size(); i++) cout << g1[i] + 1 << " "; \ for(int i = g2.size() - 1; i >= 0; i--)cout << g2[i] + 1 << " "; \ cout << "\n"; int t[4010],temp[4010],a[4010]; bool vis[4010]; vector <int> a1,a2,b1,b2; int main() { ios_base :: sync_with_stdio(0); int n; cin >> n; for(int i = 0; i < n; i++){ cin >> t[i]; temp[i] = t[i]; } sort(temp,temp +n); for(int i = 0; i < n; i++){ a[temp[i]] = i; } for(int i = 0; i < n; i++){ if(a[t[i]] != i && !vis[i] && !vis[a[t[i]]]){ a1.push_back(i); b1.push_back(a[t[i]]); vis[i] = vis[a[t[i]]] = 1; swap(t[i],t[a[t[i]]]); } } for(int i = 0; i < n; i++){ vis[i] = 0; } for(int i = 0; i < n; i++){ if(a[t[i]] != i && !vis[i] && !vis[a[t[i]]]){ a2.push_back(i); b2.push_back(a[t[i]]); vis[i] = vis[a[t[i]]] = 1; } } if(a2.size()){ cout << 2 << "\n"; print(a1,b1); print(a2,b2); } else if(a1.size()){ cout << 1 << "\n"; print(a1,b1); } else{ cout << 0 << "\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 | #include <iostream> #include <vector> #include <algorithm> using namespace std; #define print(g1,g2) \ cout << g1.size() + g2.size() << "\n"; \ for(int i = 0; i < g1.size(); i++) cout << g1[i] + 1 << " "; \ for(int i = g2.size() - 1; i >= 0; i--)cout << g2[i] + 1 << " "; \ cout << "\n"; int t[4010],temp[4010],a[4010]; bool vis[4010]; vector <int> a1,a2,b1,b2; int main() { ios_base :: sync_with_stdio(0); int n; cin >> n; for(int i = 0; i < n; i++){ cin >> t[i]; temp[i] = t[i]; } sort(temp,temp +n); for(int i = 0; i < n; i++){ a[temp[i]] = i; } for(int i = 0; i < n; i++){ if(a[t[i]] != i && !vis[i] && !vis[a[t[i]]]){ a1.push_back(i); b1.push_back(a[t[i]]); vis[i] = vis[a[t[i]]] = 1; swap(t[i],t[a[t[i]]]); } } for(int i = 0; i < n; i++){ vis[i] = 0; } for(int i = 0; i < n; i++){ if(a[t[i]] != i && !vis[i] && !vis[a[t[i]]]){ a2.push_back(i); b2.push_back(a[t[i]]); vis[i] = vis[a[t[i]]] = 1; } } if(a2.size()){ cout << 2 << "\n"; print(a1,b1); print(a2,b2); } else if(a1.size()){ cout << 1 << "\n"; print(a1,b1); } else{ cout << 0 << "\n"; } } |