[题目链接]
https://www.lydsy.com/JudgeOnline/problem.php?id=4742
[算法]
动态规划
用Fi,j,k表示约翰的前i头牛和保罗的前j头牛匹配 , 共选了k头 , 有多少种方案
转移详见代码
时间复杂度 : O(N ^ 2K)
[代码]
#include<bits/stdc++.h> using namespace std; #define MAXN 1010 #define MAXT 12 const int P = 1e9 + 9; int n , m , t; int a[MAXN] , b[MAXN]; int dp[MAXN][MAXN][MAXT]; template <typename T> inline void chkmax(T &x,T y) { x = max(x,y); } template <typename T> inline void chkmin(T &x,T y) { x = min(x,y); } template <typename T> inline void read(T &x) { T f = 1; x = 0; char c = getchar(); for (; !isdigit(c); c = getchar()) if (c == '-') f = -f; for (; isdigit(c); c = getchar()) x = (x << 3) + (x << 1) + c - '0'; x *= f; } int main() { read(n); read(m); read(t); for (int i = 1; i <= n; i++) read(a[i]); for (int i = 1; i <= m; i++) read(b[i]); sort(a + 1 , a + n + 1); sort(b + 1 , b + m + 1); dp[0][0][0] = 1; for (int i = 0; i <= n; i++) { for (int j = 0; j <= m; j++) { for (int k = 0; k <= t; k++) { if (i + j == 0) continue; if (i == 0) dp[i][j][k] = dp[i][j - 1][k]; else if (j == 0) dp[i][j][k] = dp[i - 1][j][k]; else dp[i][j][k] = dp[i - 1][j][k] + dp[i][j - 1][k] - dp[i - 1][j - 1][k]; dp[i][j][k] = (dp[i][j][k] % P + P) % P; if (k > 0 && a[i] > b[j]) dp[i][j][k] += dp[i - 1][j - 1][k - 1]; dp[i][j][k] = (dp[i][j][k] % P + P) % P; } } } printf("%d " , dp[n][m][t]); return 0; }