Constant Palindrome Sum

126 篇文章 0 订阅

题目:
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.
题解:

#include<bits/stdc++.h>
using namespace std;
int c[5000005], b[5000005], a[5000005];
int main()
{
	int t;
	cin >> t;
	while(t--)
	{
		int n, k;
		scanf("%d%d", &n, &k);
		for(int i = 1; i <= n; i++)
			scanf("%d", a + i);
		fill(c, c + 2 * k + 5, 0);
		fill(b, b + 2 * k + 5, 0);
		for(int i = 1; i <= n / 2; i++)
			c[a[i] + a[n - i + 1]]++;
		for(int i = 1; i <= n / 2; i++)
		{
			b[min(a[i], a[n - i + 1]) + 1]++;
			b[max(a[i], a[n - i + 1]) + k + 1]--;
		}
		for(int i = 3; i <= 2 * k; i++)
			b[i] += b[i - 1];
		int ans = 5000005;
		for(int x = 2; x <= 2 * k; x++)
			ans = min(ans, b[x] - c[x] + (n / 2 - b[x]) * 2);
		printf("%d\n", ans);
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值