文章目录

  • 一、B. Even Array
  • 总结

一、B. Even Array

本题链接:B. Even Array

题目

B. Even Array

time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output

You are given an array a[0…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≤i≤n−1) the equality imod2=a[i]mod2 holds, where xmod2 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: imod2=1mod2=1, but a[i]mod2=4mod2=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.

Input
The first line contains a single integer t (1≤t≤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≤n≤40) — the length of the array a.

The next line contains n integersa0,a1,…,an−1 (0≤ai≤1000) — the initial array.

Output
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.

Example
input
4
4
3 2 7 6
3
3 2 6
1
7
7
4 9 2 1 18 3 0
output
2
1
-1
0

Note
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.

本博客给出本题截图

题意:要求数组从0开始,奇数的索引必须元素为奇数,偶数的索引必须元素为偶数,问对于一个数组,每次可以交换任意两个位置上的元素,问最少交换几次可以使数组满足上述需求,如果不可以,输出-1

AC代码

#include <iostream>using namespace std;const int N = 1010;int a[N];int main()
{int t;cin >> t;while (t -- ){int n, odd = 0, even = 0;cin >> n;for (int i = 0; i < n; i ++ ){cin >> a[i];if (i % 2 == 0 && a[i] % 2 != 0)odd ++;else if (i % 2 != 0 && a[i] % 2 == 0)even ++;}if (odd != even)puts("-1");else cout << odd << endl;}return 0;
}

总结

水题,不解释

Even Array相关推荐

  1. 老男孩上海校区Python面试题

    python面试题 第一章:python基础 数据类型: 1 字典: 1.1 现有字典 dict={'a':24,'g':52,'i':12,'k':33}请按字典中的 value 值进行排序? 1. ...

  2. php recordarray,Array 数组 - [ php中文手册 ] - 在线原生手册 - php中文网

    用户评论: [#1] florenxe [2015-10-07 18:53:45] //a nice little way to print leap years using array for ($ ...

  3. NumPy — 创建全零、全1、空、arange 数组,array 对象类型,astype 转换数据类型,数组和标量以及数组之间的运算,NumPy 数组共享内存

    NumPy 简介 一个用 python 实现的科学计算包.包括: 1.一个强大的 N 维数组对象 Array : 2.比较成熟的(广播)函数库: 3.用于整合 C/C++ 和 Fortran 代码的工 ...

  4. array.array python yhzf

    关于array: Python 本身没有数组这个说法, 有的就是list和tuple, list就具有其他语言中的数组特性. 至于list和tuple的区别,在于list可以在运行时修改内容和大小,t ...

  5. [JS]请给Array本地对象增加一个原型方法,它用于删除数组条目中重复的条目(可能有多个),返回值是一个包含被删除的重复条目的新数组。

    请给Array本地对象增加一个原型方法,它用于删除数组条目中重复的条目(可能有多个),返回值是一个包含被删除的重复条目的新数组. 刚开始复习js题还不太习惯 CSDN上看了一个帖子,说是牛客上的标答, ...

  6. hnswlib RuntimeError: Cannot return the results in a contigious 2D array. Probably ef or M is to sma

    1. 问题现象 index = hnswlib.Index(space = '100', dim = 512) index.init_index(max_elements = 100, ef_cons ...

  7. Array 数组去重 总结10方法(7)

    1,常规双循环去重(缺点:循环次数较多) Array.prototype.unique1 = function(){if(this === null){throw new TypeError('&qu ...

  8. ECMAScript——引用数据类型之array

    array 转载于:https://www.cnblogs.com/cataway/p/4967058.html

  9. leetcode:Search in Rotated Sorted Array

    题目要求: Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 ...

  10. intval0.57100 php_php中0,'',null,false,true,FLASE,TREU,array()的相等恒等学习

    //比较值 '' NULL 0 false true FALSE TRUE //相等判断 //'' == NULL == 0 == false (相等) //array() = 0 == NULL = ...

最新文章

  1. linux报错 find: missing argument to `-exec'
  2. 闪念-许久未来一切没变
  3. 推荐系统依据近期浏览进行推荐
  4. 在计算机网络俗称网上邻居上能看到自己,能看到自己计算机,看不到网上邻居的标准答案...
  5. [Selenium]如何通过Selenium实现Ctrl+click,即按住Ctrl的同时进行单击操作
  6. linux shell sleep/wait(转载)
  7. 使用临时表关联数据时效率低的问题
  8. 〖Linux〗使用ssh登录远程主机,并在本地打开远程图形界面
  9. Palo Alto Networks下一代安全平台五大创新功能:云安全为重中之重
  10. 开源数据库:何为NoSQL生态系统?
  11. Linux进阶之补充知识篇
  12. SQL常见的一些面试题
  13. 华数机器人旋转编程_华数机器人离线编程软件简介与InteRobot2018 新增功能
  14. TMS570LS1224PWM的生成及捕获
  15. 优秀工程师必备的一项技能,你解锁了吗?
  16. 鹏业软件打开图纸显示内存不足问题解决
  17. 04 朴素贝叶斯法——读书笔记
  18. 2018医疗器械行业发展
  19. 关于使用Artery插件的一些积累1
  20. 服务器安全基线检查(Python)代码执行

热门文章

  1. 软件工程专业女生测试方向,女生学软件工程好吗 就业方向有哪些
  2. shell脚本执行错误
  3. 个体营业执照被吊销可以恢复吗?
  4. 以太坊学习之Java开发框架web3j的使用---部署合约
  5. 网络安全标准化工作月报
  6. 弘瑞3D打印:做更适合工业应用的3D打印机
  7. 蓝桥杯--第八届省赛试题-电子钟程序设计
  8. Prometheus监控入门级使用教程
  9. SAP 批次管理(Batch management)配置介绍
  10. PLSQL绿色版安装及配置