用vector存图+匈牙利算法
#include <iostream>
#include <cmath>
#include <vector>
#include <cstring>
using namespace std;
const int N = 1e5 + 10;
vector <int> g[1000010];
int a[N], b[N], match[N], st[N];
int find(int x)
{
for (int i = 0; i < g[x].size(); i++)
{
int t = g[x][i];
if (st[t] == 0)
{
st[t] = 1;
if (match[t] == 0 || find(match[t]))
{
match[t] = x;
return 1;
}
}
}
return 0;
}
int main()
{
int n, m,ans=0;
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
cin >> m;
for (int j = 1; j <= m; j++) cin >> b[j];
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= m; j++)
{
if (abs(a[i] - b[j]) <= 1)
g[i].push_back(j);
}
}
for (int i = 1; i <= n; i++)
{
memset(st, 0, sizeof st);
if (find(i))
{
ans++;
}
}
cout << ans;
}