# include <bits/stdc++.h> typedef int ll; using namespace std; const ll inf = 1e9 + 7; ll n , k , q; int main() { ios_base::sync_with_stdio(0); cin.tie(0); // for (int test = 1; test <= 10000; test++) { cin >> n; // n = 20; vector<ll>h1(n); for (int i = 0; i < n; i++) { cin >> h1[i]; } // Generate(h1); vector<ll>h2 = h1 , kop = h1; sort(h2.begin() , h2.end()); vector<ll>pos(3005); for (int i = 0; i < n; i++) { pos[h2[i]] = i; } vector<bool>used(n + 5); vector<vector<ll> >vec; for (int i = 0; i < n; i++) { if (used[i] || pos[h1[i]] == i)continue; ll nom = i; vector<ll>cur; while (1) { used[nom] = 1; cur.push_back(nom); nom = pos[h1[nom]]; if (used[nom])break; } if (cur.size() < 2)assert(0); vec.push_back(cur); } /* for (int i = 0; i < vec.size(); i++) { for (auto v : vec[i])cout << v + 1 << ' '; cout << endl; } */ vector<deque<ll> >res; // ll Max = -inf; while (vec.size() > 0) { deque<ll>cur_d; vector<vector<ll> >new_vec; for (int i = 0; i < vec.size(); i++) { /* Max = max(Max , (ll)vec[i].size()); if ((int)vec[i].size() < 2) { cout << -1; return 0; } */ if ((int)vec[i].size() == 2) { cur_d.push_front(vec[i][0]); cur_d.push_back(vec[i][1]); continue; } else { vector<ll>new_vi; for (int j = 0; j < vec[i].size(); j += 2) { if (j + 1 < (int)vec[i].size()) { cur_d.push_front(vec[i][j]); cur_d.push_back(vec[i][j + 1]); } new_vi.push_back(vec[i][j]); } new_vec.push_back(new_vi); } } // cout << new_vec.size() << endl; vec = new_vec; res.push_back(cur_d); } // vector<vector<ll> >ans; cout << res.size() << '\n'; for (int i = 0; i < res.size(); i++) { cout << res[i].size() << '\n'; // vector<ll>round; while (!res[i].empty()) { cout << res[i].front() + 1 << ' '; // round.push_back(res[i].front()); res[i].pop_front(); } cout << '\n'; // ans.push_back(round); } /* if (!check(ans , kop) || get_ans(Max) < res.size()) { cout << -1; return 0; } } */ } /* 4 2827 1074 2431 1152 2900 1968 2994 1633 */
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 | # include <bits/stdc++.h> typedef int ll; using namespace std; const ll inf = 1e9 + 7; ll n , k , q; int main() { ios_base::sync_with_stdio(0); cin.tie(0); // for (int test = 1; test <= 10000; test++) { cin >> n; // n = 20; vector<ll>h1(n); for (int i = 0; i < n; i++) { cin >> h1[i]; } // Generate(h1); vector<ll>h2 = h1 , kop = h1; sort(h2.begin() , h2.end()); vector<ll>pos(3005); for (int i = 0; i < n; i++) { pos[h2[i]] = i; } vector<bool>used(n + 5); vector<vector<ll> >vec; for (int i = 0; i < n; i++) { if (used[i] || pos[h1[i]] == i)continue; ll nom = i; vector<ll>cur; while (1) { used[nom] = 1; cur.push_back(nom); nom = pos[h1[nom]]; if (used[nom])break; } if (cur.size() < 2)assert(0); vec.push_back(cur); } /* for (int i = 0; i < vec.size(); i++) { for (auto v : vec[i])cout << v + 1 << ' '; cout << endl; } */ vector<deque<ll> >res; // ll Max = -inf; while (vec.size() > 0) { deque<ll>cur_d; vector<vector<ll> >new_vec; for (int i = 0; i < vec.size(); i++) { /* Max = max(Max , (ll)vec[i].size()); if ((int)vec[i].size() < 2) { cout << -1; return 0; } */ if ((int)vec[i].size() == 2) { cur_d.push_front(vec[i][0]); cur_d.push_back(vec[i][1]); continue; } else { vector<ll>new_vi; for (int j = 0; j < vec[i].size(); j += 2) { if (j + 1 < (int)vec[i].size()) { cur_d.push_front(vec[i][j]); cur_d.push_back(vec[i][j + 1]); } new_vi.push_back(vec[i][j]); } new_vec.push_back(new_vi); } } // cout << new_vec.size() << endl; vec = new_vec; res.push_back(cur_d); } // vector<vector<ll> >ans; cout << res.size() << '\n'; for (int i = 0; i < res.size(); i++) { cout << res[i].size() << '\n'; // vector<ll>round; while (!res[i].empty()) { cout << res[i].front() + 1 << ' '; // round.push_back(res[i].front()); res[i].pop_front(); } cout << '\n'; // ans.push_back(round); } /* if (!check(ans , kop) || get_ans(Max) < res.size()) { cout << -1; return 0; } } */ } /* 4 2827 1074 2431 1152 2900 1968 2994 1633 */ |