1152A A. Neko Finds Grapes

On a random day, Neko found n treasure chests and m keys. The i-th chest has an integer ai written on it and the j-th key has an integer bj on it. Neko knows those chests contain the powerful mysterious green Grapes, thus Neko wants to open as many treasure chests as possible.

The j-th key can be used to unlock the i-th chest if and only if the sum of the key number and the chest number is an odd number. Formally, ai+bj≡1(mod2). One key can be used to open at most one chest, and one chest can be opened at most once.

Find the maximum number of chests Neko can open.

Input
The first line contains integers n and m (1≤n,m≤105) — the number of chests and the number of keys.

The second line contains n integers a1,a2,…,an (1≤ai≤109) — the numbers written on the treasure chests.

The third line contains m integers b1,b2,…,bm (1≤bi≤109) — the numbers written on the keys.

Output
Print the maximum number of chests you can open.

Examples
input

5 4
9 14 6 2 11
8 4 7 20
output
3
input
5 1
2 4 6 8 10
5
output
1
input
1 4
10
20 30 40 50
output
0
Note
In the first example, one possible way to unlock 3 chests is as follows:

Use first key to unlock the fifth chest,
Use third key to unlock the second chest,
Use fourth key to unlock the first chest.
In the second example, you can use the only key to unlock any single chest (note that one key can’t be used twice).

In the third example, no key can unlock the given chest.
**題目大意:**在兩個數組中找出最多組兩者和爲奇數的對數。
思路: 找出每組的奇數個數和偶數個數,則答案爲第一組的奇數個數和第二組偶數個數的最小值加上第一組的偶數個數和第二組奇數數個數的最小值(要想得到奇數,必須是奇數加偶數)

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <set>
using namespace std;
const int N = 1e5 + 5;

int main() {
	std::ios::sync_with_stdio(false);
	cin.tie(0);
	int n, m, num, num1 = 0, num2 = 0, num3 = 0, num4 = 0;
	cin >> n >> m;
	for (int i = 0; i < n; i++) {
		cin >> num;
		if (num % 2 == 1) num1++;
		else num2++;
	}
	for (int i = 0; i < m; i++) {
		cin >> num;
		if (num % 2 == 1) num3++;
		else num4++;
	}
	int ans1 = min(num1, num4);
	int ans2 = min(num2, num3);
	int ans = ans1 + ans2;
	cout << ans;
	//system("pause");
	return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章