https://codeforces.com/problemset/problem/1343/D
题目大意:给出 n 个数,保证 n 是偶数,且每个数的范围都在 [ 1 , k ] 之间,现在问我们至少需要给多少个数重新赋值,使得可以满足条件:
- 所有的数的值域都在 [ 1 , k ] 之间
- 对于 i ∈ [ 1 , n/2 ] ,满足 a[ i ] + a[ n - i + 1 ] = x ,且所有的 x 都相等
思路:算出对于每一个 x,有多少对数可以一个都不替换,有多少可以只替换其中一个,有多少必须两个都要替换。
即转化到对于一对数,会对处于什么范围内的x产生一定的贡献。
x 处于 a[ i ] + a[ n - i + 1 ] 的话,那么这对数的贡献为 0
x 处于 [ min( a[ i ] , a[ n - i + 1 ] ) + 1 , max( a[ i ] , a[ n - i + 1 ] ) + k ] 这个范围内,那么这对数的贡献为 1
剩下x处于其他位置这对数的贡献为 2
因此我们每次循环先全部区间+2,然后对于第一个情况的区间次数贡献-1,再对剩下的第二个情况的区间次数贡献-1
#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<cstdio>
#include<algorithm>
#define debug(a) cout<<#a<<"="<<a<<endl;
using namespace std;
const int maxn=(2e5+1000)*2;
typedef int LL;
inline LL read(){LL x=0,f=1;char ch=getchar(); while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
return x*f;}
LL a[maxn];
struct Tree{
LL l,r,minval,tag;
}tree[maxn*4];
void push_up(LL p){
tree[p].minval=min(tree[p*2].minval,tree[p*2+1].minval);
}
void push_down(LL p){
if(tree[p].tag!=0){
tree[p*2].minval+=tree[p].tag;
tree[p*2+1].minval+=tree[p].tag;
tree[p*2].tag+=tree[p].tag;
tree[p*2+1].tag+=tree[p].tag;
tree[p].tag=0;
}
}
void addtag(LL p,LL d){
tree[p].minval+=d;
tree[p].tag+=d;
}
void build(LL p,LL l,LL r){
tree[p].l=l;tree[p].r=r;tree[p].tag=0;tree[p].minval=0;
if(l==r){tree[p].minval=0;return;}
LL mid=(l+r)>>1;
build(p*2,l,mid);
build(p*2+1,mid+1,r);
push_up(p);
}
void modify(LL p,LL l,LL r,LL d){
if(l<=tree[p].l&&r>=tree[p].r){
addtag(p,d);
return;
}
push_down(p);
LL mid=(tree[p].l+tree[p].r)>>1;
if(l<=mid) modify(p*2,l,r,d);
if(r>mid) modify(p*2+1,l,r,d);
push_up(p);
}
LL query(LL p,LL l,LL r){
return tree[p].minval;
}
int main(void)
{
///cin.tie(0);std::ios::sync_with_stdio(false);
LL t;t=read();
while(t--){
LL n,k;n=read();k=read();
for(LL i=1;i<=n;i++) a[i]=read();
build(1,1,2*k);
for(LL i=1;i<=n/2;i++){
modify(1,1,2*k,2);
modify(1,min(a[i],a[n-i+1])+1,max(a[i],a[n-i+1])+k,-1);
modify(1,a[i]+a[n-i+1],a[i]+a[n-i+1],-1);
}
LL ans=query(1,1,2*k);
/// debug(ans);
cout<<max(ans,(LL)0)<<"\n";
}
return 0;
}