//Solution by Mikołaj Kołek #include "bits/stdc++.h" #define intin *istream_iterator<int>(cin) using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n = intin; vector<int> h(n); copy_n(istream_iterator<int>(cin), n, h.begin()); vector<pair<int, int>> sortedH; for(int i = 0; i < n; i++) sortedH.push_back({ h[i], i }); sort(sortedH.begin(), sortedH.end()); vector<int> G(n), revG(n); for(int i = 0; i < n; i++) { G[sortedH[i].second] = i; revG[i] = sortedH[i].second; } array<deque<int>, 2> res; vector<bool> vis(n); function<bool(int, bool)> DFS = [&] (int v, int first) { if(vis[v] or G[v] == v) return false; vis[v] = true; bool val = DFS(G[v], first); if(first != v) { res[val].push_back((first != revG[v] and val) ? revG[v] : v); res[val].push_front(G[v]); } return !val; }; for(int i = 0; i < n; i++) DFS(i, i); cout << (res[1].empty() ? 1 : 2) << "\n"; for(auto &row : res) { if(!row.empty()) { cout << row.size() << "\n"; for(const auto &el : row) cout << el + 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 | //Solution by Mikołaj Kołek #include "bits/stdc++.h" #define intin *istream_iterator<int>(cin) using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n = intin; vector<int> h(n); copy_n(istream_iterator<int>(cin), n, h.begin()); vector<pair<int, int>> sortedH; for(int i = 0; i < n; i++) sortedH.push_back({ h[i], i }); sort(sortedH.begin(), sortedH.end()); vector<int> G(n), revG(n); for(int i = 0; i < n; i++) { G[sortedH[i].second] = i; revG[i] = sortedH[i].second; } array<deque<int>, 2> res; vector<bool> vis(n); function<bool(int, bool)> DFS = [&] (int v, int first) { if(vis[v] or G[v] == v) return false; vis[v] = true; bool val = DFS(G[v], first); if(first != v) { res[val].push_back((first != revG[v] and val) ? revG[v] : v); res[val].push_front(G[v]); } return !val; }; for(int i = 0; i < n; i++) DFS(i, i); cout << (res[1].empty() ? 1 : 2) << "\n"; for(auto &row : res) { if(!row.empty()) { cout << row.size() << "\n"; for(const auto &el : row) cout << el + 1 << " "; cout << "\n"; } } return 0; } |