#include <unistd.h>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <utility>
#include <deque>
#include <iostream>
#include <algorithm>
// using namespace std;
#define REP(i,n) for(int _n=(n), i=0;i<_n;++i)
#define FOR(i,a,b) for(int i=(a),_b=(b);i<=_b;++i)
#define FORD(i,a,b) for(int i=(a),_b=(b);i>=_b;--i)
#define TRACE(x) cerr << "TRACE(" #x ")" << endl;
#define DEBUG(x) cerr << #x << " = " << (x) << endl;
typedef long long LL;
typedef unsigned long long ULL;
using VINT = std::vector<int>;
using VLL = std::vector<LL>;
using VULL = std::vector<ULL>;
int main() {
std::ios_base::sync_with_stdio(false);
int n, a;
std::cin >> n;
VINT wzrost;
// , wzrost_sorted;
std::set<int> sorted;
std::map<int, int> miejsce_wzrost, wzrost_miejsce;
// std::map<int, int> wzrost_to_pos, sorted_pos;
wzrost.resize(n);
// wzrost_sorted.resize(n);
std::vector<std::vector<std::pair<int, int>>> ret;
REP(i, n) {
std::cin >> a;
sorted.insert(a);
miejsce_wzrost[i] = a;
wzrost_miejsce[a] = i;
}
int i = 0;
for (auto it : sorted) {
wzrost[wzrost_miejsce[it]] = i;
i++;
}
i = 0;
for(;;) {
std::vector<std::pair<int, int>> local_ret;
bool is_sorted = true;
std::set<int> visited;
REP(i, n) {
if (visited.find(i) != visited.end())
continue;
if (wzrost[i] == i) continue;
is_sorted = false;
int obecne = i;
for (;;) {
if(visited.find(obecne) != visited.end())
break;
visited.insert(obecne);
auto docelowe = wzrost[obecne];
if (docelowe == i || visited.find(docelowe) != visited.end()) {
break;
}
visited.insert(docelowe);
int tmp = wzrost[docelowe];
std::swap(wzrost[obecne], wzrost[docelowe]);
local_ret.push_back(std::make_pair(obecne, docelowe));
obecne = tmp;
}
}
if (is_sorted) break;
ret.push_back(local_ret);
}
std::cout << ret.size() << std::endl;
for(auto &a : ret) {
std::cout << a.size() * 2 << std::endl;
for (auto &aa : a)
std::cout << aa.first+1 << " ";
for (auto it = a.rbegin(); it != a.rend(); it++) {
std::cout << it->second+1 << " ";
}
std::cout << std::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 | #include <unistd.h> #include <string> #include <vector> #include <map> #include <set> #include <utility> #include <deque> #include <iostream> #include <algorithm> // using namespace std; #define REP(i,n) for(int _n=(n), i=0;i<_n;++i) #define FOR(i,a,b) for(int i=(a),_b=(b);i<=_b;++i) #define FORD(i,a,b) for(int i=(a),_b=(b);i>=_b;--i) #define TRACE(x) cerr << "TRACE(" #x ")" << endl; #define DEBUG(x) cerr << #x << " = " << (x) << endl; typedef long long LL; typedef unsigned long long ULL; using VINT = std::vector<int>; using VLL = std::vector<LL>; using VULL = std::vector<ULL>; int main() { std::ios_base::sync_with_stdio(false); int n, a; std::cin >> n; VINT wzrost; // , wzrost_sorted; std::set<int> sorted; std::map<int, int> miejsce_wzrost, wzrost_miejsce; // std::map<int, int> wzrost_to_pos, sorted_pos; wzrost.resize(n); // wzrost_sorted.resize(n); std::vector<std::vector<std::pair<int, int>>> ret; REP(i, n) { std::cin >> a; sorted.insert(a); miejsce_wzrost[i] = a; wzrost_miejsce[a] = i; } int i = 0; for (auto it : sorted) { wzrost[wzrost_miejsce[it]] = i; i++; } i = 0; for(;;) { std::vector<std::pair<int, int>> local_ret; bool is_sorted = true; std::set<int> visited; REP(i, n) { if (visited.find(i) != visited.end()) continue; if (wzrost[i] == i) continue; is_sorted = false; int obecne = i; for (;;) { if(visited.find(obecne) != visited.end()) break; visited.insert(obecne); auto docelowe = wzrost[obecne]; if (docelowe == i || visited.find(docelowe) != visited.end()) { break; } visited.insert(docelowe); int tmp = wzrost[docelowe]; std::swap(wzrost[obecne], wzrost[docelowe]); local_ret.push_back(std::make_pair(obecne, docelowe)); obecne = tmp; } } if (is_sorted) break; ret.push_back(local_ret); } std::cout << ret.size() << std::endl; for(auto &a : ret) { std::cout << a.size() * 2 << std::endl; for (auto &aa : a) std::cout << aa.first+1 << " "; for (auto it = a.rbegin(); it != a.rend(); it++) { std::cout << it->second+1 << " "; } std::cout << std::endl; } return 0; } |
English