Cyclic Shifts Sorting

You are given an array a consisting of nn integers.

In one move, you can choose some index i(1in2)i (1≤i≤n−2) and shift the segment [ai,ai+1,ai+2][a_i,a_{i+1},a_{i+2}] cyclically to the right (i.e. replace the segment [ai,ai+1,ai+2][a_i,a_{i+1},a_{i+2}] with [ai+2,ai,ai+1][a_{i+2},a_i,a_{i+1}]).

Your task is to sort the initial array by no more than n2n_2 such operations or say that it is impossible to do that.

You have to answer t independent test cases.

Input
The first line of the input contains one integer t(1t100)t (1≤t≤100) — the number of test cases. Then t test cases follow.

The first line of the test case contains one integer n(3n500)n (3≤n≤500) — the length of a. The second line of the test case contains n integers a1,a2,,an(1ai500)a_1,a_2,…,a_n (1≤a_i≤500), where aia_i is the ii-th element a.

It is guaranteed that the sum of n does not exceed 500500.

Output
For each test case, print the answer: 1-1 on the only line if it is impossible to sort the given array using operations described in the problem statement, or the number of operations ans on the first line and ansans integers idx1,idx2,,idxansidx_1,idx_2,…,idx_{ans} (1idxin2)(1≤idx_i≤n−2), where idxiidx_i is the index of left border of the segment for the ii-th operation. You should print indices in order of performing operations.

Example

input
5
5
1 2 3 4 5
5
5 4 3 2 1
8
8 4 5 2 3 6 7 3
7
5 2 1 6 4 7 3
6
1 2 3 3 6 4
output
0

6
3 1 3 2 2 3 
13
2 1 1 6 4 2 4 3 3 4 4 6 6 
-1
4
3 3 4 4 

代碼先貼上,題解坑留到考完試後再填

#include<bits/stdc++.h>

#define si(a) scanf("%d",&a)
#define sl(a) scanf("%lld",&a)
#define sd(a) scanf("%lf",&a)
#define sc(a) scahf("%c",&a);
#define ss(a) scanf("%s",a)
#define pi(a) printf("%d\n",a)
#define pl(a) printf("%lld\n",a)
#define pc(a) putchar(a)
#define ms(a) memset(a,0,sizeof(a))
#define repi(i, a, b) for(register int i=a;i<=b;++i)
#define repd(i, a, b) for(register int i=a;i>=b;--i)
#define reps(s) for(register int i=head[s];i;i=Next[i])
#define ll long long
#define ull unsigned long long
#define vi vector<int>
#define pii pair<int,int>
#define mii unordered_map<int,int>
#define msi unordered_map<string,int>
#define lowbit(x) ((x)&(-(x)))
#define ce(i, r) i==r?'\n':' '
#define pb push_back
#define fi first
#define se second
#define INF 0x3f3f3f3f
#define pr(x) cout<<#x<<": "<<x<<endl
using namespace std;

inline int qr() {
    int f = 0, fu = 1;
    char c = getchar();
    while (c < '0' || c > '9') {
        if (c == '-')fu = -1;
        c = getchar();
    }
    while (c >= '0' && c <= '9') {
        f = (f << 3) + (f << 1) + c - 48;
        c = getchar();
    }
    return f * fu;
}

const int N = 505;
int T, n, m;
vi ans, seq;
int a[N];
int now;

inline void move(int x) {
    int pos;
    now++;
    if (m - now == 1) {
        if (a[1] > a[2]) {
            if (m == 2) {
                puts("-1");
                return;
            }
            if (a[2] == seq[now - 2])ans.pb(now - 1);
            else {
                int j = -1;
                repd(i, now - 2, 1)if (seq[i] == seq[i - 1]) {
                        j = i + 2;
                        break;
                    }
                if (j == -1) {
                    puts("-1");
                    return;
                }
                if ((now & 1) == (j & 1)) {
                    for (int i = now - 2; i >= j; i -= 2)ans.pb(i);
                    for (int i = now - 1; i >= j + 1; i -= 2)ans.pb(i);
                    ans.pb(j - 2), ans.pb(j - 2);
                    ans.pb(j - 1), ans.pb(j - 1);
                    for (int i = j + 1; i <= now - 1; i += 2)ans.pb(i), ans.pb(i);
                    for (int i = j; i <= now - 2; i += 2)ans.pb(i), ans.pb(i);
                } else {
                    for (int i = now - 1; i >= j; i -= 2)ans.pb(i);
                    for (int i = now - 1; i >= j; i -= 2)ans.pb(i);
                    ans.pb(j - 2), ans.pb(j - 2), ans.pb(j - 1);
                    for (int i = j; i <= now - 1; i += 2)ans.pb(i), ans.pb(i);
                    for (int i = j - 1; i <= now - 2; i += 2)ans.pb(i), ans.pb(i);
                }
            }
        }
        pi(ans.size());
        for (auto it:ans)printf("%d ", it);
        puts("");
        return;
    }
    repi(i, 1, n)if (a[i] == x) {
            pos = i;
            break;
        }
    n--;
    repi(i, pos, n)a[i] = a[i + 1];
    for (int i = now + pos - 3; i >= now; i -= 2)ans.pb(i);
    if (!(pos & 1)) {
        ans.pb(now), ans.pb(now);
        swap(a[1], a[2]);
    }
}

int main() {
    T = qr();
    while (T--) {
        ans.clear(), seq.clear(), now = 0;
        n = qr(), m = n;
        repi(i, 1, n)a[i] = qr(), seq.pb(a[i]);
        if (n == 1) {
            puts("0\n");
            continue;
        }
        sort(seq.begin(), seq.end());
        repi(i, 0, m - 2)move(seq[i]);
    }
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章