#include <bits/stdc++.h> typedef long long ll; using namespace std; vector<pair<int,int>> s; unordered_map<int,int> m; int height[3010]; int pos[3010]; int good[3010]; deque<int> ans; deque<int> ans2; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n; cin>>n; for(int i=0; i<n; i++) { cin>>height[i]; s.push_back({height[i],i}); } sort(s.begin(),s.end()); for(auto i:s) { m[i.first*4000+i.second]=m.size(); } for(int i=0; i<n; i++) { height[i]=m[height[i]*4000+i]; pos[height[i]]=i; } for(int i=0; i<n; i++) { // cout<<height[i]<<" "<<pos[i]<<" "<<good[height[i]]<<" "<<good[pos[i]]<<"\n"; if(height[i]==pos[i]||good[height[i]]||good[pos[i]]) continue; int idx=i; while(true) { // cout<<"idx "<<idx<<"\n"; if(height[idx]==pos[idx]||good[height[idx]]||good[pos[idx]]) break; good[height[idx]]=1; good[pos[idx]]=1; ans.push_front(height[idx]); ans.push_back(pos[idx]); swap(height[height[idx]],height[pos[idx]]); int oli = idx; idx=pos[idx]; swap(pos[height[height[oli]]],pos[height[pos[oli]]]); } } for(int i=0; i<n;i++) { if(height[i]==i) continue; ans2.push_front(height[i]); ans2.push_back(i); swap(height[height[i]],height[i]); } cout<<2-ans.empty()-ans2.empty()<<"\n"; if(!ans.empty()) { cout<<ans.size()<<"\n"; for(auto i:ans) { cout<<i+1<<" "; } cout<<"\n"; } if(!ans2.empty()) { cout<<ans2.size()<<"\n"; for(auto i:ans2) { cout<<i+1<<" "; } } }
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 | #include <bits/stdc++.h> typedef long long ll; using namespace std; vector<pair<int,int>> s; unordered_map<int,int> m; int height[3010]; int pos[3010]; int good[3010]; deque<int> ans; deque<int> ans2; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n; cin>>n; for(int i=0; i<n; i++) { cin>>height[i]; s.push_back({height[i],i}); } sort(s.begin(),s.end()); for(auto i:s) { m[i.first*4000+i.second]=m.size(); } for(int i=0; i<n; i++) { height[i]=m[height[i]*4000+i]; pos[height[i]]=i; } for(int i=0; i<n; i++) { // cout<<height[i]<<" "<<pos[i]<<" "<<good[height[i]]<<" "<<good[pos[i]]<<"\n"; if(height[i]==pos[i]||good[height[i]]||good[pos[i]]) continue; int idx=i; while(true) { // cout<<"idx "<<idx<<"\n"; if(height[idx]==pos[idx]||good[height[idx]]||good[pos[idx]]) break; good[height[idx]]=1; good[pos[idx]]=1; ans.push_front(height[idx]); ans.push_back(pos[idx]); swap(height[height[idx]],height[pos[idx]]); int oli = idx; idx=pos[idx]; swap(pos[height[height[oli]]],pos[height[pos[oli]]]); } } for(int i=0; i<n;i++) { if(height[i]==i) continue; ans2.push_front(height[i]); ans2.push_back(i); swap(height[height[i]],height[i]); } cout<<2-ans.empty()-ans2.empty()<<"\n"; if(!ans.empty()) { cout<<ans.size()<<"\n"; for(auto i:ans) { cout<<i+1<<" "; } cout<<"\n"; } if(!ans2.empty()) { cout<<ans2.size()<<"\n"; for(auto i:ans2) { cout<<i+1<<" "; } } } |