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;
  }
}





相关文章
|
2天前
|
监控 网络协议 算法
2026年起SSL证书有效期逐年缩短:企业如何避免“续期风暴”!
SSL证书有效期将从2026年起分三阶段逐步缩短至47天,人工续期难以为继。阿里云“WoSign SSL证书+自动化服务”,支持域名监控、自动托管、云产品/服务器一键部署及智能提醒,助力企业高效应对高频续期挑战。
8077 2
|
11天前
|
域名解析 人工智能 Linux
|
19天前
|
人工智能
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置
进不去 Antigravity 或 Gemini,提示所在地区不可用?三步查清 Google 账号关联地区、官方更正表单理由选法与改区注意事项。
10264 13
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置
|
5天前
|
人工智能 安全 网络安全
|
10天前
|
Linux API iOS开发
【DSH】DeepSeek Harness新手保姆教程(下载+安装+配置+使用,2026最新实测)
DeepSeek Harness(dsh)是 DeepSeek AI 开源的智能体框架,秉持“一切皆插件”理念,支持读文件、改代码、执行命令、处理表格等自动化任务;免费开源,兼容 Win/macOS,需配 API 密钥使用。(239字)
1547 5
|
9天前
|
云安全 数据采集 人工智能
|
16天前
|
人工智能 开发工具 开发者
2026最新codex下载安装保姆级使用教程(下载+安装+登录+使用,含所有历史版本)
Codex 是 OpenAI 推出的开源 AI 编程智能体,跑在本地终端和桌面客户端里,能读懂整个代码仓库,一句自然语言就能让它改代码、执行命令、跑测试、修 Bug,支持 Windows、macOS、Linux 三大平台。本文把 Codex下载、安装、登录、汉化、基础使用一次讲全,并整理了国内用户最常踩的坑,跟着做 10 分钟就能跑起来。
2696 8
|
9天前
|
Web App开发 缓存 安全
巨魔商店 TrollStore 安装教程(2026):免电脑在线安装与全机型安装器矩阵_iOS巨魔安装
巨魔商店(TrollStore)是iOS 14–17.0(特定版本)的永久签名工具,无需越狱,利用CoreTrust漏洞实现IPA签名长期有效,彻底告别7天重签。支持免电脑在线安装(TrollHelperOTA),适配多机型/系统组合,开源免费,安全可控。(239字)
2379 0