#include<bits/stdc++.h> using namespace std; int MAX_N = 3000; struct Node{ int value; int currentIndex; int targetIndex; bool inCircle; bool iterated = false; Node(int value, int currentIndex){ this->value = value; this->currentIndex = currentIndex; this->inCircle = false; } }; struct CircleGraph{ Node* fst; bool completed = false; int iterationIndex = 0; int size; CircleGraph(Node* fst){ this->fst = fst; this->size = 1; } CircleGraph(Node* fst, int size){ this->fst = fst; this->size = size; } Node* next(vector<Node*>& nodes, Node* current=NULL, int step=1){ if(current==NULL) return fst; for(int i=0; i<step; i++){ current = nodes[current->targetIndex]; } return current; } }; int main(){ int n; cin >> n; vector<Node*> nodes(n); map<int ,Node*> input; int temp; for(int i=0; i<n; i++){ cin >> temp; Node *new_node = new Node(temp, i); nodes[i] = new_node; input[temp] = new_node; } int targetIndex = 0; for(int x=1; x<=MAX_N; x++){ auto it = input.find(x); if(it != input.end()){ it->second->targetIndex = targetIndex; targetIndex++; } } vector<CircleGraph*> circles; for(auto node: nodes){ if(!node->inCircle && node->currentIndex != node->targetIndex){ CircleGraph *newCircle = new CircleGraph(node); circles.push_back(newCircle); node->inCircle=true; auto nextNode = nodes[node->targetIndex]; while(nextNode != node){ nextNode->inCircle=true; newCircle->size++; nextNode = nodes[nextNode->targetIndex]; } } } bool completed = false; vector< vector<int> > results; while(!completed){ completed = true; vector<int> selected; vector<int> targets; for(int i=0; i<circles.size(); i++){ CircleGraph *circle = circles[i]; completed &= circle->completed; if(!circle->completed){ Node *first = circle->next(nodes); Node *split = circle->next(nodes, first, circle->size/2-1); Node *last = circle->next(nodes, split, circle->size-circle->size/2); if(last->iterated){ first = circle->next(nodes, first); split = circle->next(nodes, split); last = circle->next(nodes, last); } if(!split->iterated && !last->iterated){ selected.push_back(split->currentIndex+1); targets.push_back(last->currentIndex+1); split->iterated = true; last->iterated = true; if(circle->size > 2){ Node *nextToSplit = circle->next(nodes, split); CircleGraph *newCircle2 = new CircleGraph(nextToSplit, circle->size/2 + circle->size%2); circles.push_back(newCircle2); } if(circle->size > 3){ CircleGraph *newCircle1 = new CircleGraph(first, circle->size/2); circles.push_back(newCircle1); } swap(nodes[split->currentIndex], nodes[last->currentIndex]); swap(split->currentIndex, last->currentIndex); circle->completed = true; } } } if(!completed){ for(int i=targets.size()-1; i>=0; i--){ selected.push_back(targets[i]); } results.push_back(selected); for(auto x: nodes){ x->iterated = false; } } } cout << results.size() << "\n"; for(auto& result: results){ cout << result.size() << "\n"; for(auto& x: result){ cout << x << " "; } cout << "\n"; } }
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 | #include<bits/stdc++.h> using namespace std; int MAX_N = 3000; struct Node{ int value; int currentIndex; int targetIndex; bool inCircle; bool iterated = false; Node(int value, int currentIndex){ this->value = value; this->currentIndex = currentIndex; this->inCircle = false; } }; struct CircleGraph{ Node* fst; bool completed = false; int iterationIndex = 0; int size; CircleGraph(Node* fst){ this->fst = fst; this->size = 1; } CircleGraph(Node* fst, int size){ this->fst = fst; this->size = size; } Node* next(vector<Node*>& nodes, Node* current=NULL, int step=1){ if(current==NULL) return fst; for(int i=0; i<step; i++){ current = nodes[current->targetIndex]; } return current; } }; int main(){ int n; cin >> n; vector<Node*> nodes(n); map<int ,Node*> input; int temp; for(int i=0; i<n; i++){ cin >> temp; Node *new_node = new Node(temp, i); nodes[i] = new_node; input[temp] = new_node; } int targetIndex = 0; for(int x=1; x<=MAX_N; x++){ auto it = input.find(x); if(it != input.end()){ it->second->targetIndex = targetIndex; targetIndex++; } } vector<CircleGraph*> circles; for(auto node: nodes){ if(!node->inCircle && node->currentIndex != node->targetIndex){ CircleGraph *newCircle = new CircleGraph(node); circles.push_back(newCircle); node->inCircle=true; auto nextNode = nodes[node->targetIndex]; while(nextNode != node){ nextNode->inCircle=true; newCircle->size++; nextNode = nodes[nextNode->targetIndex]; } } } bool completed = false; vector< vector<int> > results; while(!completed){ completed = true; vector<int> selected; vector<int> targets; for(int i=0; i<circles.size(); i++){ CircleGraph *circle = circles[i]; completed &= circle->completed; if(!circle->completed){ Node *first = circle->next(nodes); Node *split = circle->next(nodes, first, circle->size/2-1); Node *last = circle->next(nodes, split, circle->size-circle->size/2); if(last->iterated){ first = circle->next(nodes, first); split = circle->next(nodes, split); last = circle->next(nodes, last); } if(!split->iterated && !last->iterated){ selected.push_back(split->currentIndex+1); targets.push_back(last->currentIndex+1); split->iterated = true; last->iterated = true; if(circle->size > 2){ Node *nextToSplit = circle->next(nodes, split); CircleGraph *newCircle2 = new CircleGraph(nextToSplit, circle->size/2 + circle->size%2); circles.push_back(newCircle2); } if(circle->size > 3){ CircleGraph *newCircle1 = new CircleGraph(first, circle->size/2); circles.push_back(newCircle1); } swap(nodes[split->currentIndex], nodes[last->currentIndex]); swap(split->currentIndex, last->currentIndex); circle->completed = true; } } } if(!completed){ for(int i=targets.size()-1; i>=0; i--){ selected.push_back(targets[i]); } results.push_back(selected); for(auto x: nodes){ x->iterated = false; } } } cout << results.size() << "\n"; for(auto& result: results){ cout << result.size() << "\n"; for(auto& x: result){ cout << x << " "; } cout << "\n"; } } |