CF1556B.Take Your Places!

普及-

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

William has an array of nn integers a1,a2,…,ana_1, a_2, \dots, a_n. In one move he can swap two neighboring items. Two items aia_i and aja_j 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.

威廉有一个由 nn 个整数组成的数组 a1,a2,…,ana_1, a_2, \dots, a_n。在一次操作中,他可以交换两个相邻的元素。当且仅当 ∣i−j∣=1|i - j| = 1 时,元素 aia_i 和 aja_j 被视为相邻。

威廉希望你计算出:为使数组中不存在两个相邻元素具有相同奇偶性(即同为奇数或同为偶数),他至少需要执行多少次交换操作。

输入格式

Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤1041 \le t \le 10^4). Description of the test cases follows.

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

The second line contains nn integers a1,a2,…,ana_1, a_2, \dots, a_n (1≤ai≤109)(1 \le a_i \le 10^9) which are William's array.

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

每个测试包含多个测试用例。第一行包含测试用例的数量 tt(1≤t≤1041 \le t \le 10^4)。随后是测试用例的描述。

每个测试用例的第一行包含一个整数 nn(1≤n≤1051 \le n \le 10^5),表示威廉数组中元素的总数。

第二行包含 nn 个整数 a1,a2,…,ana_1, a_2, \dots, a_n(1≤ai≤1091 \le a_i \le 10^9),即威廉的数组。

保证所有测试用例的 nn 之和不超过 10510^5。

输出格式

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.

对于每个测试用例,输出所需的最少操作次数;如果无法使数组满足“任意相邻两个数的奇偶性均不同”的状态,则输出 −1-1。

输入输出样例

  • 输入#1

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

    输出#1

    1
    0
    3
    -1
    2

说明/提示

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. swap(2, 3)。执行该操作后的数组:[6,1,6][6, 1, 6]

在第二个测试用例中,初始数组中不存在两个相邻的同奇偶性元素。

在第三个测试用例中,以下操作序列可满足要求:

  1. swap(3, 4)。执行该操作后的数组:[1,1,2,1,2,2][1, 1, 2, 1, 2, 2]
  2. swap(2, 3)。执行该操作后的数组:[1,2,1,1,2,2][1, 2, 1, 1, 2, 2]
  3. swap(4, 5)。执行该操作后的数组:[1,2,1,2,1,2][1, 2, 1, 2, 1, 2]

在第四个测试用例中,无法满足要求。

在第五个测试用例中,以下操作序列可满足要求:

  1. swap(2, 3)。执行该操作后的数组:[6,3,2,4,5,1][6, 3, 2, 4, 5, 1]
  2. swap(4, 5)。执行该操作后的数组:[6,3,2,5,4,1][6, 3, 2, 5, 4, 1]

输入解题思路,AI测评打分。不知道怎么写?

首页