#include <bits/stdc++.h>
using namespace std;
vector <pair<int, int>> start;
vector <pair<int, int>> ending;
bitset<3007> used;
bitset<3007> git;
deque <int> q;
vector <vector<int>> ciag[30000];
vector <deque<int>> odp;
int n, h, x = 1, y, maxi, done;
void findCycles(){
used.reset();
x = 1;
maxi = 0;
while(x <= n){
vector<int> temp;
if(!git[ending[x-1].first] && !used[ending[x-1].first]){
used[ending[x-1].first] = true;
temp.push_back(ending[x-1].first);
y = ending[x-1].second;
while(!used[ending[y-1].first]){
used[ending[y-1].first] = true;
temp.push_back(ending[y-1].first);
y = ending[y-1].second;
}
ciag[temp.size()].push_back(temp);
if(temp.size() > maxi)
maxi = temp.size();
}
x++;
}
}
void check(){
done = 0;
for(int i = 0; i < n; i++){
if(ending[i].first == ending[i].second){
git[ending[i].second] = true;
done++;
}
}
}
void wypisz(){
for(int i = 0; i < n; i++){
cout << ending[i].first << " ";
}
cout << endl;
for(int i = 0; i < n; i++){
cout << ending[i].second << " ";
}
cout << endl;
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for(int i = 0; i < n; i++){
cin >> h;
start.push_back({h, i+1});
}
sort(start.begin(), start.end());
for(int i = 0; i < n; i++){
ending.push_back({start[i].second, i+1});
}
sort(ending.begin(), ending.end());
//oznacz te które są na swoich miejsach
check();
// wypisz();
// return 0;
// cout << endl << endl;
//chyba lepsza wersja findcycles?
while(done != n){
used.reset();
x = 1;
maxi = 0;
while(x <= n){
if(!git[ending[x-1].second] && !git[ending[x-1].first] && !used[ending[x-1].first] && !used[ending[x-1].second]){
used[ending[x-1].first] = true;
used[ending[x-1].second] = true;
q.push_front(ending[x-1].first);
q.push_back(ending[x-1].second);
// cout << q.front() << " "
swap(ending[q.front()-1].second, ending[q.back()-1].second);
}
x++;
}
if(q.size() != 0)
odp.push_back(q);
q.clear();
check();
}
// while(done != n){
// findCycles();
// for(int i = 2; i <= maxi; i++){
// if(ciag[i].size() != 0){
// for(auto e : ciag[i]){
// for(int k = 0; k < e.size()/2; k++){
// q.push_front(e[2*k]);
// q.push_back(e[2*k + 1]);
// }
// }
// }
// }
// odp.push_back(q);
// for(auto e : q){
// cout << e << " ";
// }
// cout << endl;
// while(!q.empty()){
// swap(ending[q.front()-1].second, ending[q.back()-1].second);
// q.pop_back();
// q.pop_front();
// }
// check();
// wypisz();
// cout << endl;
// }
cout << odp.size() << endl;
for(int i = 0; i < odp.size(); i++){
cout << odp[i].size() << endl;
for(int j = 0; j < odp[i].size(); j++){
cout << odp[i][j] << " ";
}
cout << endl;
}
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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 | #include <bits/stdc++.h> using namespace std; vector <pair<int, int>> start; vector <pair<int, int>> ending; bitset<3007> used; bitset<3007> git; deque <int> q; vector <vector<int>> ciag[30000]; vector <deque<int>> odp; int n, h, x = 1, y, maxi, done; void findCycles(){ used.reset(); x = 1; maxi = 0; while(x <= n){ vector<int> temp; if(!git[ending[x-1].first] && !used[ending[x-1].first]){ used[ending[x-1].first] = true; temp.push_back(ending[x-1].first); y = ending[x-1].second; while(!used[ending[y-1].first]){ used[ending[y-1].first] = true; temp.push_back(ending[y-1].first); y = ending[y-1].second; } ciag[temp.size()].push_back(temp); if(temp.size() > maxi) maxi = temp.size(); } x++; } } void check(){ done = 0; for(int i = 0; i < n; i++){ if(ending[i].first == ending[i].second){ git[ending[i].second] = true; done++; } } } void wypisz(){ for(int i = 0; i < n; i++){ cout << ending[i].first << " "; } cout << endl; for(int i = 0; i < n; i++){ cout << ending[i].second << " "; } cout << endl; } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cin >> n; for(int i = 0; i < n; i++){ cin >> h; start.push_back({h, i+1}); } sort(start.begin(), start.end()); for(int i = 0; i < n; i++){ ending.push_back({start[i].second, i+1}); } sort(ending.begin(), ending.end()); //oznacz te które są na swoich miejsach check(); // wypisz(); // return 0; // cout << endl << endl; //chyba lepsza wersja findcycles? while(done != n){ used.reset(); x = 1; maxi = 0; while(x <= n){ if(!git[ending[x-1].second] && !git[ending[x-1].first] && !used[ending[x-1].first] && !used[ending[x-1].second]){ used[ending[x-1].first] = true; used[ending[x-1].second] = true; q.push_front(ending[x-1].first); q.push_back(ending[x-1].second); // cout << q.front() << " " swap(ending[q.front()-1].second, ending[q.back()-1].second); } x++; } if(q.size() != 0) odp.push_back(q); q.clear(); check(); } // while(done != n){ // findCycles(); // for(int i = 2; i <= maxi; i++){ // if(ciag[i].size() != 0){ // for(auto e : ciag[i]){ // for(int k = 0; k < e.size()/2; k++){ // q.push_front(e[2*k]); // q.push_back(e[2*k + 1]); // } // } // } // } // odp.push_back(q); // for(auto e : q){ // cout << e << " "; // } // cout << endl; // while(!q.empty()){ // swap(ending[q.front()-1].second, ending[q.back()-1].second); // q.pop_back(); // q.pop_front(); // } // check(); // wypisz(); // cout << endl; // } cout << odp.size() << endl; for(int i = 0; i < odp.size(); i++){ cout << odp[i].size() << endl; for(int j = 0; j < odp[i].size(); j++){ cout << odp[i][j] << " "; } cout << endl; } return 0; } |
English