Codeforces Round #646 (Div. 2) A. Odd Selection(数学)

题目链接:https://codeforces.com/contest/1363/problem/A

题意

判断是否能从 $n$ 个数中选 $x$ 个数加起来和为奇数。

题解

首先 $n$ 个数中至少需要有 $1$ 个奇数,之后为了不影响和的奇偶性向余下 $x-1$ 个数中加入成对的奇数或单个偶数即可。

代码

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

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

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

 

posted @ 2020-06-01 10:10  Kanoon  阅读(278)  评论(0编辑  收藏  举报