Deltix Round, Summer 2021 (open for everyone, rated, Div. 1 + Div. 2)B. Take Your Places!

B. Take Your Places!

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

William has an array of nn integers a1,a2,…,ana1,a2,…,an. In one move he can swap two neighboring items. Two items aiai and ajaj are considered neighboring if the condition |i−j|=1|i−j|=1 is satisfied.

William wants you to calculate the minimal number of swaps he would need to perform to make it so that the array does not contain two neighboring items with the same parity.

Input

Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤1041≤t≤104). Description of the test cases follows.

The first line of each test case contains an integer nn (1≤n≤105)(1≤n≤105) which is the total number of items in William's array.

The second line contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤109)(1≤ai≤109) which are William's array.

It is guaranteed that the sum of nn over all test cases does not exceed 105105.

Output

For each test case output the minimal number of operations needed or −1−1 if it is impossible to get the array to a state when no neighboring numbers have the same parity.

Example

input

Copy

5
3
6 6 1
1
9
6
1 1 1 2 2 2
2
8 6
6
6 2 3 4 5 1

output

Copy

1
0
3
-1
2

Note

In the first test case the following sequence of operations would satisfy the requirements:

  1. swap(2, 3). Array after performing the operation: [6,1,6][6,1,6]

In the second test case the array initially does not contain two neighboring items of the same parity.

In the third test case the following sequence of operations would satisfy the requirements:

  1. swap(3, 4). Array after performing the operation: [1,1,2,1,2,2][1,1,2,1,2,2]
  2. swap(2, 3). Array after performing the operation: [1,2,1,1,2,2][1,2,1,1,2,2]
  3. swap(4, 5). Array after performing the operation: [1,2,1,2,1,2][1,2,1,2,1,2]

In the fourth test case it is impossible to satisfy the requirements.

In the fifth test case the following sequence of operations would satisfy the requirements:

  1. swap(2, 3). Array after performing the operation: [6,3,2,4,5,1][6,3,2,4,5,1]
  2. swap(4, 5). Array after performing the operation: [6,3,2,5,4,1][6,3,2,5,4,1]

题意分析,有这些情况

1奇数比偶数多一个数的话,反之同理,

2奇数偶数数量相等的时候取和的最小值 

3,否则就不能组成排列

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;

const int N = 3e5 + 10, mod = 1e9 + 7;

int n, m, t, k;

int s[N], dp[N], f[N];
char g[N];

string str;

vector<int> vec;

void solve() {
	vec.clear();
	cin >> n;
	int a = 0, b = 0, c = 0, d = 0;
	for (int i = 1; i <= n; i++) {
		cin >> s[i];
		if (s[i] & 1) {
			a++;
			vec.push_back(i);
		} else
			b++;
	}
	if (abs(a - b) > 1) {
		puts("-1");
		return;
	}
	if (a == b) {
		int ans = 0, ans1 = 0;
		for (int i = 0; i < vec.size(); i++) {
			ans += abs(vec[i] - (2 * i + 1));
			ans1 += abs(vec[i] - (2 * i + 2));
		}
		printf("%d\n", min(ans, ans1));
		return;
	}
	if (a > b) {
		int ans = 0;
		for (int i = 0; i < vec.size(); i++)
			ans += abs(vec[i] - (2 * i + 1));
		printf("%d\n", ans);
		return;
	}
	int ans = 0;
	for (int i = 0; i < vec.size(); i++)
		ans += abs(vec[i] - (2 * i + 2));
	printf("%d\n", ans);
}

int main() {
	t = 1;
	cin >> t;
	while (t--)
		solve();
	return 0;
}

下面这个借鉴的,很简洁 

#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 1e5 + 5; 
int a ;

int main() {
	int t;
	scanf("%d", &t);
	while (t--) {
		int n;
		scanf("%d", &n);
		vector<int> v;
		for (int i = 1; i <= n; i++) {
			cin >> a ;
			if (a  & 1)//奇数
				v.push_back(i);
		}
		int len = v.size();//奇数个数
		int m = n - len;//偶数个数
		if (labs(len - m) > 1) {//无法组成排列
			cout << -1 << endl;
			continue;
		}
		ll ans = 1e15 + 5, cnt = 0, p = 0;
		if (len - m == 1 || len - m == 0) { //如果奇数比偶数多1,或者相等;
			for (int i = 1; i <= n; i += 2) {
				cnt += labs(v[p++] - i);
			}
			ans = min(ans, cnt);
		}
		cnt = 0;
		p = 0;
		if (m - len == 1 || m - len == 0) {
			for (int i = 2; i <= n; i += 2) {
				cnt += labs(v[p++] - i);
			}
			ans = min(ans, cnt);
		}
		cout << ans << endl;
	}
}

评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

算法编程张老师

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

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

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

打赏作者

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

抵扣说明:

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

余额充值