一看就是贪心,想了个策略,先排序再尽可能用两个较小的凑成一对,
代码:
#include<iostream>
#include<cstdio>
#include<cmath>
#include<map>
#include<cstring>
#include<algorithm>
#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define rev(i,a,b) for(int i=(a);i>=(b);i--)
#define clr(a,x) memset(a,x,sizeof a)
typedef long long LL;
using namespace std;
const int mod=1e9 +7;
const int maxn=3005;
const int maxm=4005;
int da[maxn],db[maxn],u[maxn],v[maxn];
int dis(int a,int b)
{
return a>b?a-b:b-a;
}
int main()
{
int n,m;
while(~scanf("%d",&n))
{
for(int i=0;i<n;i++)
scanf("%d",&da[i]);
scanf("%d",&m);
for(int i=0;i<m;i++)
scanf("%d",&db[i]);
sort(da,da+n);
sort(db,db+m);
int cur=0,cnt=0;
for(int i=0;cur<m&&i<n;)
{
while(cur<m&&da[i]-db[cur]>1)
cur++;
while(i<n&&db[cur]-da[i]>1)
i++;
if(dis(db[cur],da[i])<=1)cnt++,cur++,i++;
}
printf("%d\n",cnt);
}
return 0;
}