Codeforces Round #650 (Div. 3) B. Even Array

题目链接:https://codeforces.com/contest/1367/problem/B

题意

有一大小为 $n$ 的数组 $a$,问能否经过交换使所有元素与下标奇偶性相同(0 - indexed)。

题解

奇偶性不同的奇数和偶数个数应相等。

代码

#include <bits/stdc++.h>
using namespace std;

void solve() {
    int n; cin >> n;
    int odd = 0, even = 0;
    for (int i = 0; i < n; i++) {
        int x; cin >> x;
        if ((x - i) & 1) {
            if (x & 1) ++odd;
            else ++even;
        }
    }
    cout << (odd == even ? odd : -1) << "\n";
}

int main() {
    int t; cin >> t;
    while (t--) solve();
}

 

posted @ 2020-06-17 23:12  Kanoon  阅读(255)  评论(0编辑  收藏  举报