Codeforces Round #646 (Div. 2) A

Codeforces Round #646 (Div. 2) A

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

题目大意:问能否从n个数中选出x个数使得他们的和为奇数

思路:记录数组中奇数的个数k、偶数的个数s,我们只需要保证这x个数中k为奇数就行了。

#include<iostream>
#include<algorithm>
using namespace std;
 
const int N = 1005;
 
int main()
{
	int t, n, x;
	int a[N];
	cin >> t;
	while (t--) {
		cin >> n >> x;
		int k = 0, s = 0; // k 为奇数的个数 s为偶数的个数。
		for (int i = 0; i < n; i++) {
			cin >> a[i];
			if (a[i] % 2 == 0)
				s++;
			else
				k++;
		}
		if (k > x)
			k = x;
		if (k > 0) {
			if (k % 2 == 0)
				k--;
			k += s;
		}
		if (k >= x) {
			cout << "Yes" << endl;
		}
		else
			cout << "No" << endl;
	}
	return 0;
}
原文地址:https://www.cnblogs.com/woyaoAC/p/14059466.html