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





相关文章
|
9月前
|
机器学习/深度学习 编解码 自动驾驶
RT-DETR改进策略【模型轻量化】| 替换骨干网络为MoblieNetV1,用于移动视觉应用的高效卷积神经网络
RT-DETR改进策略【模型轻量化】| 替换骨干网络为MoblieNetV1,用于移动视觉应用的高效卷积神经网络
361 3
RT-DETR改进策略【模型轻量化】| 替换骨干网络为MoblieNetV1,用于移动视觉应用的高效卷积神经网络
|
8月前
|
存储 机器学习/深度学习 缓存
🚀 力扣热题 394:字符串解码(详细解析)(Go语言版)
文章提供了两种解法:栈结构和递归解法。栈解法通过维护数字栈与字符串栈,依次处理 `[` 和 `]`,构造解码结果;递归解法则利用函数调用逐层解析嵌套结构。两者时间复杂度均为 $O(n)$,空间复杂度也为 $O(n)$。栈解法直观易懂,适合初学者;递归解法优雅简洁,适合处理深度嵌套规则。掌握这两种方法,可灵活应对类似问题,提升解题能力。
282 11
|
SQL 数据采集 JSON
使用对比!SLS 数据加工 SPL 与旧版 DSL 场景对照
本文讨论在不同的数据处理需求中,新版数据加工 SPL 与旧版数据加工 DSL 的使用对照。
7757 95
|
Ubuntu 测试技术 网络安全
Ubuntu系统下部署flatpress轻量级博客系统
【10月更文挑战第3天】Ubuntu系统下部署flatpress轻量级博客系统
229 3
Ubuntu系统下部署flatpress轻量级博客系统
|
Ubuntu Linux 网络安全
linux系统ubuntu中在命令行中打开图形界面的文件夹
在Ubuntu系统中,通过命令行打开图形界面的文件夹是一个高效且实用的操作。无论是使用Nautilus、Dolphin还是Thunar,都可以根据具体桌面环境选择合适的文件管理器。通过上述命令和方法,可以简化日常工作,提高效率。同时,解决权限问题和图形界面问题也能确保操作的顺利进行。掌握这些技巧,可以使Linux操作更加便捷和灵活。
411 3
|
编译器 Linux 程序员
深度探索Linux操作系统 —— 构建工具链
深度探索Linux操作系统 —— 构建工具链
277 5
|
缓存 负载均衡 Dubbo
Dubbo技术深度解析及其在Java中的实战应用
Dubbo是一款由阿里巴巴开源的高性能、轻量级的Java分布式服务框架,它致力于提供高性能和透明化的RPC远程服务调用方案,以及SOA服务治理方案。
404 6
|
网络协议 测试技术 网络安全
Python进行Socket接口测试的实现
在现代软件开发中,网络通信是不可或缺的一部分。无论是传输数据、获取信息还是实现实时通讯,都离不开可靠的网络连接和有效的数据交换机制。而在网络编程的基础中,Socket(套接字)技术扮演了重要角色。 Socket 允许计算机上的程序通过网络进行通信,它是网络通信的基础。Python 提供了强大且易于使用的 socket 模块,使开发者能够轻松地创建客户端和服务器应用,实现数据传输和交互。 本文将深入探讨如何利用 Python 编程语言来进行 Socket 接口测试。我们将从基础概念开始介绍,逐步引导大家掌握创建、测试和优化 socket 接口的关键技能。希望本文可以给大家的工作带来一些帮助~
|
图形学
【unity小技巧】实现FPS武器的瞄准放大效果(UGUI实现反向遮罩,全屏遮挡,局部镂空效果)
【unity小技巧】实现FPS武器的瞄准放大效果(UGUI实现反向遮罩,全屏遮挡,局部镂空效果)
469 1