Codeforces Round #636 (Div. 3) D. Constant Palindrome Sum

You are given an array a consisting of n integers (it is guaranteed that n is even, i.e. divisible by 2). All ai does not exceed some integer k.

Your task is to replace the minimum number of elements (replacement is the following operation: choose some index i from 1 to n and replace ai with some integer in range [1;k]) to satisfy the following conditions:

after all replacements, all ai are positive integers not greater than k;
for all i from 1 to n2 the following equation is true: ai+an−i+1=x, where x should be the same for all n2 pairs of elements.
You have to answer t independent test cases.

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

The first line of the test case contains two integers n and k (2≤n≤2⋅105,1≤k≤2⋅105) — the length of a and the maximum possible value of some ai correspondingly. It is guratanteed that n is even (i.e. divisible by 2). The second line of the test case contains n integers a1,a2,…,an (1≤ai≤k), where ai is the i-th element of a.

It is guaranteed that the sum of n (as well as the sum of k) over all test cases does not exceed 2⋅105 (∑n≤2⋅105, ∑k≤2⋅105).

Output
For each test case, print the answer — the minimum number of elements you have to replace in a to satisfy the conditions from the problem statement.

Example
inputCopy
4
4 2
1 2 1 2
4 3
1 2 2 1
8 7
6 1 1 7 6 3 4 6
6 6
5 2 6 1 3 4
outputCopy
0
1
4
2

树状数组复习

int lowbit(int x) {
    return x&-x;
}
void add(int x,int d) {
    for(int i=x; i<=2*k; i+=lowbit(i))
        tr[i]+=d;
}
ll sum(int x) {
    ll res=0;
    for(int i=x; i; i-=lowbit(i))
        res+=tr[i];
    return res;
}
int main() {
    scanf( "%d", &t );
    while(t--) {
        scanf( "%d%d",&n, &k );
        map<int,int> mp;
        for(int i=1; i<=2*k; i++)
            tr[i]=0;
        for(int i=1; i<=n; i++)
            scanf( "%d",&a[i] );
        ll res=0;
        for(int i=1; i<=n/2; i++) {
            l[i]=min(a[i],a[n-i+1])+1;
            r[i]=max(a[i],a[n-i+1])+k;
            int s=a[i]+a[n-i+1];
            add(1,2);
            add(l[i],-2);
            add(l[i],1);
            add(s,-1);
            add(s+1,1);
            add(r[i]+1,-1);
            add(r[i]+1,2);
        }
        res=0x3f3f3f3f;
        for(int i=2; i<=2*k; i++)
            res=min(res,sum(i));
        cout<<res<<endl;
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值