欢迎来到起遇信息学
起遇信息学正处于上线筹建阶段,以下功能已全部开放免费体验: ✅ 完整题库浏览与代码提交评测(C / C++ / Python / Java 等) ✅ 入门到进阶的系列课程试读、作业与考试 ✅ AI 提示、AI 作业分析等智能助教功能 ✅ 赛事模拟与个人能力报告 ✅ 邮箱注册开放 ⏳ 付费课程订阅与微信/支付宝支付通道 ⏳ 手机号登录,微信扫码登录、微信公众号绑定 使用中如遇任何问题,欢迎通过页面底部 **"联系我们"** 与我们沟通。
CF1367B.Even Array
Even Array
You are given an array of length 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 () the equality holds, where is the remainder of dividing by 2.
For example, the arrays [] and [] are good, and the array [] is bad, because for , the parities of and are different: , but .
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 good, or say that this is not possible.
Input
The first line contains a single integer () — the number of test cases in the test. Then test cases follow.
Each test case starts with a line containing an integer () — the length of the array .
The next line contains integers () — the initial array.
Output
For each test case, output a single integer — the minimum number of moves to make the given array good, or -1 if this is not possible.
Note
In the first test case, in the first move, you can swap the elements with indices and , and in the second move, you can swap the elements with indices and .
In the second test case, in the first move, you need to swap the elements with indices and .
In the third test case, you cannot make the array good.
Samples
4
4
3 2 7 6
3
3 2 6
1
7
7
4 9 2 1 18 3 0
2
1
-1
0
在线编程 IDE
建议全屏模式获得最佳体验
| 进入全屏编程 | Alt+E |
| 递交评测 | Ctrl+Enter |
| 注释/取消注释 | Ctrl+/ |
| 缩放字体 | Ctrl+滚轮 |