贪心Thanks, TuSimple!

题意为:男的找比他矮的或比他高的,女的要找比他矮的或比他高的,0代表找个矮的,1代表找个高的
计算最大配对数
贪心找到
匹配首先 男女肯定要不是01 或者10,这个是必须的然后在寻找符合条件的,当然要从小到大排序一个一个找,如果找到匹配的,下一个人一定在他找到的那个人的后面找,这样才能找到最大匹配数
代码如下

#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn = 100050;
struct node
{
    int heigh;
    int select;
}a[maxn],b[maxn];
bool cmp(node x,node y)
{
    if(x.select == y.select)
        return x.heigh < y.heigh;
    else x.select < y.select;
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n,m;
        scanf("%d %d",&n,&m);
        for(int i = 0; i < n; ++i)
            scanf("%d",&a[i].heigh);
        for(int i = 0; i < m; ++i )
            scanf("%d",&b[i].heigh);
        int cnt1 = 0,cnt2 = 0;
        for(int i = 0; i < n; ++i)
        {
            scanf("%d",&a[i].select);
            if(!a[i].select)
                ++cnt1;//男生0的个数
        }
        for(int i = 0; i < m; ++i)
        {
            scanf("%d",&b[i].select);
            if(!b[i].select)
                ++cnt2;
        }
        sort(a,a+n,cmp);
        sort(b,b+m,cmp);
        int num = 0;
        int j = cnt2;
        for(int i = 0; i < cnt1; ++i)//女生高男生矮
        {
            if(a[i].heigh > b[j].heigh)
            {
                num++;
                j++;
            }
            if(j >= m)
                break;
        }
        j = cnt1;
        for(int i = 0; i < cnt2; ++i)
        {
            if(b[i].heigh > a[j] .heigh)
            {
                num++;
                j++;
            }
            if(j >= n)
                break;
        }
        cout<<num<<'\n';
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值