第一次cf经历A. Neko Finds Grapes

本文介绍了一种算法挑战,Neko在一个随机的日子找到了n个宝藏箱和m把钥匙,目标是通过匹配钥匙和箱子上的数字,尽可能多地打开宝藏箱。文章详细解释了算法流程,包括输入输出格式,以及如何确定最大可打开的宝藏箱数量。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

A. Neko Finds Grapes
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
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.
题目链接

#include<bits/stdc++.h>
#define maxn 101000
using namespace std;
int main()
{
    long long  a[maxn],b[maxn];
    long long  m,n,sum1=0,sum2=0;
    scanf("%d %d",&n,&m);
    for(int i=0;i<n;i++)
    {
        scanf("%lld",&a[i]);
        a[i]%=2;
        sum1+=a[i];
    }
    for(int i=0;i<m;i++)
    {
        scanf("%lld",&b[i]);
        b[i]%=2;
        sum2+=b[i];
    }
    long long  cnt=0;
    cnt=min(sum1,m-sum2)+min(sum2,n-sum1);
    if(cnt>n) cnt=n;
    printf("%lld\n",cnt);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值