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

简介: 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:


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

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月前
|
前端开发 开发者
Warning: [antd: Breadcrumb] `Breadcrumb.Item and Breadcrumb.Separator` is deprecated. Please use `it
Warning: [antd: Breadcrumb] `Breadcrumb.Item and Breadcrumb.Separator` is deprecated. Please use `it
336 1
Deltix Round, Summer 2021 (open for everyone, rated, Div. 1 + Div. 2)
Deltix Round, Summer 2021 (open for everyone, rated, Div. 1 + Div. 2)
61 0
|
机器学习/深度学习 人工智能 算法
Cannot find source code based button in SE24
When you are logging on to customer system for incident handling, you want to switch to source code to perform some keyword search. However, you could not find button “Source code based builder” in toolbar, with following warning message: ———————————————— 版权声明:本文为CSDN博主「汪子熙」的原创文章,遵循CC 4.0 BY-SA版权协
Cannot find source code based button in SE24
|
C#
Template、ItemsPanel、ItemContainerStyle、ItemTemplate
原文:Template、ItemsPanel、ItemContainerStyle、ItemTemplate 先来看一张图(网上下的图,加了几个字) 实在是有够“乱”的,慢慢来理一下; 1、Template是指控件的样式 在WPF中所有继承自contentcontrol类的控件都含有此属性,(继承自FrameworkElementdl类的TextBlock等控件无)。
739 0