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 integers a0,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
inputCopy
4
4
3 2 7 6
3
3 2 6
1
7
7
4 9 2 1 18 3 0
outputCopy
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.

链接:https://codeforces.ml/problemset/problem/1367/B
题意:一个数组中所有i%2要和a[i]%2相等则为good 如果能,输出交换的次数,如果不相等,看看能否通过交换使得数组为good 不能则输出-1
思路:只需要统计能不能的次数,就只需要去统计就好了,通过发现可以知道数组需要 偶数 奇数 偶数 奇数… 所以判断a[i]是否为对应的偶数或者奇数,如果不是则统计,判断最后统计出来的奇偶是否相等
ac代码如下:

#include<iostream>
#include<cstring>
using namespace std;
int main()
{int t,a[1002];cin>>t;while(t--){int n,cou1=0,cou2=0;cin>>n;for(int i=0;i<n;i++){cin>>a[i];}for(int i=0;i<n;i+=2){if(a[i]%2!=0) cou1++;}for(int i=1;i<n;i+=2){if(a[i]%2!=1) cou2++;}if(cou1==cou2) cout<<cou1<<endl;else cout<<-1<<endl;memset(a,0,sizeof(a));}
}

codeforces 1367B - Even Array相关推荐

  1. Codeforces 1054D Changing Array

    Codeforces 1054D Changing Array 做法:给定一个序列,每个数可以把在2进制k位下取反,也可以不变,在改变后,这个序列异或和不为0的区间的个数.考虑如何求出尽可能少的异或为 ...

  2. CodeForces - 1610B Kalindrome Array

    B. Kalindrome Array time limit per test1 second memory limit per test256 megabytes An array [b1,b2,- ...

  3. codeforces EDU suffix array

    本文以codeforces EDU suffix array为资料 content introdcution suffix array height array lcp problems & ...

  4. [codeforces 1343B] Balanced Array 奇+奇=偶,奇+偶=奇,偶+偶=偶

    Codeforces Round #636 (Div. 3)   比赛人数12253 [codeforces 1343B]   Balanced Array   奇+奇=偶,奇+偶=奇,偶+偶=偶 总 ...

  5. CodeForces - 1539F Strange Array(线段树区间合并)

    题目链接:点击查看 题目大意:给出一个长度为 nnn 的序列,规定位置 iii 的贡献是:设 x=a[i]x=a[i]x=a[i],选择一个包含 iii 的区间 [l,r][l,r][l,r],将其中 ...

  6. CodeForces - 1534E Lost Array(bfs+交互)

    题目链接:点击查看 题目大意:初始时给出一个长度为 nnn 的序列,每次可以询问 kkk 个位置的异或和,现在需要以最少的询问获得整个序列的异或和 题目分析:因为是异或,我们只关心每个位置被询问的次数 ...

  7. 【Codeforces】808D Array Division(前后两部分和相等)

    http://codeforces.com/contest/808/problem/D 给你一个数组,问:是否可以通过移动一个数字的位置,求只能移动一次,使得这个数组前后部分的和相等,前后部分不一定等 ...

  8. Codeforces 494D Upgrading Array

    http://codeforces.com/contest/494/problem/D 题意:给一个数组,和一个坏质数集合,可以无数次地让1到i这些所有数字除以他们的gcd,然后要求Σf(a[i])的 ...

  9. CodeForces - 817D Imbalanced Array(单调栈)

    题目链接:点击查看 题目大意:给出一个长度为 nnn 的序列,求出所有子区间的最大值与最小值之差的和 题目分析:不难看出最大值和最小值之差的和可以拆开,拆成最大值之和与最小值之和之差,现在问题转换为如 ...

最新文章

  1. mysql切换到使用openssl_OpenSSL可以用来调试到MySQL服务器的SSL连接吗?
  2. override java field_JavaPoet 看这一篇就够了
  3. 启明云端WT516P6Core离线语音模块发布后,开发者朋友提出的问题最多的是:是否可以自己编译指令
  4. java class api 中文乱码_SpringMVC之springmvc原始api,请求中文乱码问题
  5. 硅谷创业者中被遮蔽的“中国现象”
  6. 数据库中字段类型Number(n,m)大概说明
  7. 颜宁的学生都已经副院长了!还发表了学校首篇Nature
  8. 【数据库】数据库基础
  9. Sping Environment为Null的原因和解决方法
  10. 设备管理系统未来发展的四大趋势
  11. OFD文件解析流程(转)
  12. div 设置a4大小_如何在A4纸张尺寸页面中制作HTML页面?
  13. US1M-ASEMI高效恢复二极管US1M
  14. 兴趣专业测试软件,测试你的专业兴趣是什么
  15. BUUCTF misc 专题(82)[WUSTCTF2020]alison_likes_jojo
  16. Altium中PCB板上开窗
  17. 密码锁屏保护隐私更安全,这款口碑好的手机浏览器值得拥有
  18. 著名全球最大同性交友社区网站
  19. Damn it! 又忘记VSS Admin的密码了!
  20. c语言程序设计试题汇编第三版勘误,c语言程序设计基础教程----勘误记录.pdf

热门文章

  1. TYPEC 接口芯片CC逻辑原理与必要性
  2. jupyter(ipython)内嵌问题:%pylab inline的使用
  3. 做完基线后centos /linux 系统修改密码报passwd: Module is unknown 未知模块 解决思路
  4. [计算机组成原理]2-6、算数移位、逻辑移位、循环移位
  5. Maya2014下载安装与激活
  6. istio 实战 六 全链路监控 - Jaeger
  7. 我对光学相干层析成像的理解
  8. 常用js的数组方法和对象方法
  9. Datadome 最新逆向分析(支持爱马仕)
  10. php反转图片颜色,PHP 图片处理类(水印、透明度、缩放、相框、锐化、旋转、翻转、剪切、反色)...