B. BerSU Ball
排序后考虑\(dp\),\(dp_{i,j}\)表示前\(i\)个男生和前\(j\)个女生能匹配的最大数.
\[\begin{aligned} if(|a_i - b_j| \le 1) \ dp_{i,j} = dp_{i-1,j-1} + 1 \\ else \ \ \ \ \ \ \ \ dp_{i,j}=\max(dp_{i-1,j},dp_{i,j-1}) \end{aligned} \]#include <bits/stdc++.h>
#define debug(a) cout<<#a<<"="<<a<<'\n';
using namespace std;
using i64 = long long;
typedef pair<i64, i64> PII;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, m;
cin >> n ;
vector<int> a(n + 1);
for (int i = 1; i <= n; i ++ )
cin >> a[i];
cin >> m;
vector<int> b(m + 1);
for (int j = 1; j <= m; j ++)
cin >> b[j];
sort(a.begin() + 1, a.end());
sort(b.begin() + 1, b.end());
vector dp(n + 1, vector<int>(m + 1));
for (int i = 1; i <= n; i ++) {
for (int j = 1; j <= m; j ++) {
if (abs(a[i] - b[j]) <= 1)
dp[i][j] = dp[i - 1][j - 1] + 1;
else
dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
}
}
cout << dp[n][m] << '\n';
return 0;
}
标签:begin,Ball,end,int,vector,BerSU,dp
From: https://www.cnblogs.com/Kescholar/p/17871592.html