From left to right, put each number in place in turn. Put a number in the right place, observe the discovery of up to two steps
Assuming the operation to the first position, and I this number is exactly in the POS position, now it is necessary to determine whether the POS can be directly transferred to the position I
If i + (pos-i) * 2-1 <= N, the operation can be completed one time
In cases where the above conditions are not established, there are two different cases
One is that the distance between POS and I is odd: then the value of the interval [I,pos] is exchanged directly
The other is the case where the distance is even, then the value of this interval [I+1,pos] is exchanged
AC Code:
#include <iostream> #include <cstdio> #include <cstdlib> #include <cctype> #include <cstring > #include <string> #include <sstream> #include <vector> #include <set> #include <map># Include <algorithm> #include <stack> #include <queue> #include <bitset> #include <cassert > #include <cmath> #include <functional>using namespace std;const int maxn = 10005;int numbers[maxn];void E Xchange (int L, int R) {for (int i = l, j = l + (r-l + 1)/2;j <= R; j + +, i++) {swap (numbers[i], numbers[j]);}} int main () {Ios::sync_with_stdio (false), int t;cin >> t;while (t--) {int n;cin >> n;for (int i = 1; I <= n; i + +) {cin >> numbers[i];} Vector<pair<int, int> > ans;for (int i = 1; I <= n; i++) {int pos;for (int j = i; J <= N; j + +) {if (numb ERS[J] = = i) {//Find position of I pos = J;break;}} if (pos = = i) {continue;} if (i + 2 * (pos-i)-1 <= N) {//satisfy this condition can be directly ans.push_back (Make_pair (i, i + 2* (Pos-i)-1); Exchange (I, i + 2 * (pos-i)-1);} else {if ((pos-i)% 2) {Ans.push_back (Make_pair (i, POS)); Exchange (I, POS);} else {ans.push_back (Make_pair (i + 1, POS)); Exchange (i + 1, POS);} i--;}} cout << ans.size () << endl;for (int i = 0; i < ans.size (); i++) {cout << ans[i].first << ' &L t;< ans[i].second << Endl;}} return 0;}
Uva-1611-crane