Constant Palindrome Sum CodeForces - 1343D(树状数组)

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
Input
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
Output
0
1
4
2
这个题目真的很不错。
思路:一开始考虑的是贪心,但是丝毫没有头绪,感觉情况特别多,没法想。后来看到标签有一个是“暴力”。暴力难道就是把所有的情况都考虑一遍,然后求其中的最小值?那么如何去求所有情况的代价呢?我们可以发现,对于每一对来说,有三种情况,一种是都改变,一种是改变1个,还有一个是什么都不改变。设每一对的和为x,这一对数之间小的为_min,大的为_max,和为sum。
当x在[2,_min]时,需要改变2个。
当x在[_max+k+1,2*k+1]时,需要改变2个。
当x在[_min+1,_max+k]时,需要改变1个。
当x等于sum时,不需要改变。
这样思路就很明确了,差分或者树状数组。我采用的树状数组。
在这里插入图片描述
代码如下:

#include<bits/stdc++.h>
#define ll long long
#define inf 0x3f3f3f3f
using namespace std;

const int maxx=2e5+100;
int c[maxx<<1];
int a[maxx];
int n,k;

inline int lowbit(int x){return x&-x;}
inline void update(int x,int v)
{
	while(x<2*k+99)
	{
		c[x]+=v;
		x+=lowbit(x);
	}
}
inline int query(int x)
{
	int ans=0;
	while(x)
	{
		ans+=c[x];
		x-=lowbit(x);
	}
	return ans;
}
int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d%d",&n,&k);
		for(int i=1;i<=n;i++) scanf("%d",&a[i]);
		for(int i=0;i<2*k+99;i++) c[i]=0;
		int sum=0;
		int _min,_max;
		for(int i=1;i<=n/2;i++)
		{
			_min=min(a[i],a[n-i+1]);
			_max=max(a[i],a[n-i+1]);
			sum=_min+_max;
			update(2,2);
			update(_min+1,-2);
			update(_max+k+1,2);
			update(2*k+1,-2);
			update(_min+1,1);
			update(_max+k+1,-1);
			update(sum,-1);//当x==sum的时候,说明不需要改变,那么就在sum上-1,sum+1上面+1.
			update(sum+1,1);
		}
		int ans=inf;
		for(int i=2;i<=2*k;i++) ans=min(ans,query(i));
		cout<<ans<<endl;
	}
	return 0;
}

努力加油a啊,(o)/~

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

starlet_kiss

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

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

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

打赏作者

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

抵扣说明:

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

余额充值