Cyclic Shifts Sorting

You are given an array a consisting of n n n integers.

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

Your task is to sort the initial array by no more than n 2 n_2 n2 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 ( 1 ≤ t ≤ 100 ) t (1≤t≤100) t(1t100) — the number of test cases. Then t test cases follow.

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

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

Output
For each test case, print the answer: − 1 -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 a n s ans ans integers i d x 1 , i d x 2 , … , i d x a n s idx_1,idx_2,…,idx_{ans} idx1,idx2,,idxans ( 1 ≤ i d x i ≤ n − 2 ) (1≤idx_i≤n−2) (1idxin2), where i d x i idx_i idxi is the index of left border of the segment for the i i i-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 

首先,对于前 n − 2 n-2 n2小的数,一定存在方法将其由小到大依次移动到特定位置,这样就把前 n − 2 n-2 n2小的数排好了。对于后两个数,如果顺序递增,则已经排好,否则要么两个数中较小的与数在已排好的序列里存在或者已排好的序列里有重复的元素,都存在策略使其排好,如果两种情况都没有,则不能排好。

#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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

_sky123_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值