A13502 | Even Array
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
You are given an array $a[0 \ldots n-1]$ of length $n$ which consists of non-negative integers. Note that array indices start from zero.
An array is called good if the parity of each index matches the parity of the element at that index. More formally, an array is good if for all $i$ ( $0 \le i \le n - 1$ ) the equality $i \bmod 2 = a[i] \bmod 2$ holds, where $x \bmod 2$ is the remainder of dividing $x$ by 2.
For example, the arrays \[ $0, 5, 2, 1$ \] and \[ $0, 17, 0, 3$ \] are good, and the array \[ $2, 4, 6, 7$ \] is bad, because for $i=1$ , the parities of $i$ and $a[i]$ are different: $i \bmod 2 = 1 \bmod 2 = 1$ , but $a[i] \bmod 2 = 4 \bmod 2 = 0$ .
In one move, you can take any two elements of the array and swap them (these elements are not necessarily adjacent).
Find the minimum number of moves in which you can make the array $a$ good, or say that this is not possible.
An array is called good if the parity of each index matches the parity of the element at that index. More formally, an array is good if for all $i$ ( $0 \le i \le n - 1$ ) the equality $i \bmod 2 = a[i] \bmod 2$ holds, where $x \bmod 2$ is the remainder of dividing $x$ by 2.
For example, the arrays \[ $0, 5, 2, 1$ \] and \[ $0, 17, 0, 3$ \] are good, and the array \[ $2, 4, 6, 7$ \] is bad, because for $i=1$ , the parities of $i$ and $a[i]$ are different: $i \bmod 2 = 1 \bmod 2 = 1$ , but $a[i] \bmod 2 = 4 \bmod 2 = 0$ .
In one move, you can take any two elements of the array and swap them (these elements are not necessarily adjacent).
Find the minimum number of moves in which you can make the array $a$ good, or say that this is not possible.
输入格式
The first line contains a single integer $t$ ( $1 \le t \le 1000$ ) — the number of test cases in the test. Then $t$ test cases follow.
Each test case starts with a line containing an integer $n$ ( $1 \le n \le 40$ ) — the length of the array $a$ .
The next line contains $n$ integers $a_0, a_1, \ldots, a_{n-1}$ ( $0 \le a_i \le 1000$ ) — the initial array.
Each test case starts with a line containing an integer $n$ ( $1 \le n \le 40$ ) — the length of the array $a$ .
The next line contains $n$ integers $a_0, a_1, \ldots, a_{n-1}$ ( $0 \le a_i \le 1000$ ) — the initial array.
输出格式
For each test case, output a single integer — the minimum number of moves to make the given array $a$ good, or -1 if this is not possible.
输入输出样例
输入 #1
4 4 3 2 7 6 3 3 2 6 1 7 7 4 9 2 1 18 3 0
输出 #1
2 1 -1 0
In the first test case, in the first move, you can swap the elements with indices $0$ and $1$ , and in the second move, you can swap the elements with indices $2$ and $3$ .
In the second test case, in the first move, you need to swap the elements with indices $0$ and $1$ .
In the third test case, you cannot make the array good.
In the second test case, in the first move, you need to swap the elements with indices $0$ and $1$ .
In the third test case, you cannot make the array good.
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted