#include<bits/stdc++.h> using namespace std; //g++ -O3 -static fot_C3.cpp -std=c++11 -o bin/fot_C3 //./bin/fot_C3 /* 5 4 5 3 1 2 */ int n; pair<int, int> hwo[3010]; bool visited[3010]; vector<vector<pair<int, int> > >rounds; int main(){ ios_base::sync_with_stdio(0); cin.tie(0); vector<pair<int, int> > round_0; rounds.push_back(round_0); int n; cin >> n; for(int i = 0; i < n; i++){ int height; cin >> height; hwo[i] = {height, i}; } sort(hwo, hwo + n); for(int i = 0; i < n; i++){ if (!visited[i]) { vector<int> v; v.push_back(i); visited[i] = true; int curr = hwo[i].second; while (curr != i) { v.push_back(curr); visited[curr] = true; curr = hwo[curr].second; } //cout << "size " << v.size() << endl; reverse(v.begin(), v.end()); //cout << "print v" << endl; // for (auto x : v) { // cout << "x " << x << endl; // } //cout << "v size " <<v.size() << endl; if (v.size() == 1) { continue; } else if (v.size() == 2) { rounds[0].push_back({v[0], v[1]}); } else { if (rounds.size() == 1) { vector<pair<int, int> > round; rounds.push_back(round); } int pocz = 1; int kon = v.size() - 1; while (kon > pocz) { rounds[0].push_back({v[pocz], v[kon]}); pocz++; kon--; } rounds[1].push_back({v[0], v[1]}); pocz = 2; kon = v.size() - 1; while (kon > pocz) { rounds[1].push_back({v[pocz], v[kon]}); pocz++; kon--; } } } } cout << rounds.size(); for (vector<pair<int, int> > round : rounds) { cout << "\n" << 2 * round.size() << "\n"; for (int i = 0; i < round.size(); ++i) { cout << round[i].first + 1 << " "; } for (int i = round.size() - 1; i >= 0; --i) { cout << round[i].second + 1 << " "; } } 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 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> using namespace std; //g++ -O3 -static fot_C3.cpp -std=c++11 -o bin/fot_C3 //./bin/fot_C3 /* 5 4 5 3 1 2 */ int n; pair<int, int> hwo[3010]; bool visited[3010]; vector<vector<pair<int, int> > >rounds; int main(){ ios_base::sync_with_stdio(0); cin.tie(0); vector<pair<int, int> > round_0; rounds.push_back(round_0); int n; cin >> n; for(int i = 0; i < n; i++){ int height; cin >> height; hwo[i] = {height, i}; } sort(hwo, hwo + n); for(int i = 0; i < n; i++){ if (!visited[i]) { vector<int> v; v.push_back(i); visited[i] = true; int curr = hwo[i].second; while (curr != i) { v.push_back(curr); visited[curr] = true; curr = hwo[curr].second; } //cout << "size " << v.size() << endl; reverse(v.begin(), v.end()); //cout << "print v" << endl; // for (auto x : v) { // cout << "x " << x << endl; // } //cout << "v size " <<v.size() << endl; if (v.size() == 1) { continue; } else if (v.size() == 2) { rounds[0].push_back({v[0], v[1]}); } else { if (rounds.size() == 1) { vector<pair<int, int> > round; rounds.push_back(round); } int pocz = 1; int kon = v.size() - 1; while (kon > pocz) { rounds[0].push_back({v[pocz], v[kon]}); pocz++; kon--; } rounds[1].push_back({v[0], v[1]}); pocz = 2; kon = v.size() - 1; while (kon > pocz) { rounds[1].push_back({v[pocz], v[kon]}); pocz++; kon--; } } } } cout << rounds.size(); for (vector<pair<int, int> > round : rounds) { cout << "\n" << 2 * round.size() << "\n"; for (int i = 0; i < round.size(); ++i) { cout << round[i].first + 1 << " "; } for (int i = round.size() - 1; i >= 0; --i) { cout << round[i].second + 1 << " "; } } return 0; } |