#include <iostream> #include <vector> #include <algorithm> #include <map> #include <unordered_set> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n; cin >> n; map<int,int> SA = {}; vector<int> S(n); for(int i = 0; i < n; i++) { cin >> S[i]; SA[S[i]] = i; } vector<int> SS(S); sort(SS.begin(), SS.end()); map<int, map<int,bool>> SM = {}; vector<int> SC(S); map<int, unordered_set<int>> SZ = {}; int oz = 0; for(int i = 0; i < n; i++) { int ia = SA[SS[i]]; if(SC[ia] == SC[i]) { continue; } if(SM.find(i-1) != SM.end()) { SZ[oz].insert(i); SZ[oz].insert(ia); }else oz++; SM[i][ia] = true; swap(SC[i], SC[ia]); swap(SA[SC[i]], SA[SC[ia]]); } for(int i = 0; i <= oz; i++) { if(SZ.find(i) == SZ.end()){ continue; } auto szs = SZ[i]; if(szs.size() == 0){ cerr << "0-sized\n"; continue; } cout << szs.size() << "\n"; for(auto j = szs.begin(); j != szs.end(); j++) { cout << *j << " "; } cout << "\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 58 59 60 61 62 63 64 65 66 67 68 69 70 | #include <iostream> #include <vector> #include <algorithm> #include <map> #include <unordered_set> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n; cin >> n; map<int,int> SA = {}; vector<int> S(n); for(int i = 0; i < n; i++) { cin >> S[i]; SA[S[i]] = i; } vector<int> SS(S); sort(SS.begin(), SS.end()); map<int, map<int,bool>> SM = {}; vector<int> SC(S); map<int, unordered_set<int>> SZ = {}; int oz = 0; for(int i = 0; i < n; i++) { int ia = SA[SS[i]]; if(SC[ia] == SC[i]) { continue; } if(SM.find(i-1) != SM.end()) { SZ[oz].insert(i); SZ[oz].insert(ia); }else oz++; SM[i][ia] = true; swap(SC[i], SC[ia]); swap(SA[SC[i]], SA[SC[ia]]); } for(int i = 0; i <= oz; i++) { if(SZ.find(i) == SZ.end()){ continue; } auto szs = SZ[i]; if(szs.size() == 0){ cerr << "0-sized\n"; continue; } cout << szs.size() << "\n"; for(auto j = szs.begin(); j != szs.end(); j++) { cout << *j << " "; } cout << "\n"; } } |