题意为:男的找比他矮的或比他高的,女的要找比他矮的或比他高的,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;
}