usaco

Clean implementations of solutions to USACO problems

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  18. 18
  19. 19
  20. 20
  21. 21
  22. 22
  23. 23
  24. 24
  25. 25
  26. 26
  27. 27
  28. 28
  29. 29
  30. 30
  31. 31
  32. 32
  33. 33
  34. 34
  35. 35
  36. 36
  37. 37
  38. 38
  39. 39
  40. 40
  41. 41
  42. 42
  43. 43
  44. 44
  45. 45
  46. 46
  47. 47
#include <bits/stdc++.h>
#define f first
#define s second
using namespace std;
using ll = long long;
using ii = pair<int, int>;
const int MX = 3005, MOD = 1e9+7;


ll s[MX], t[MX], DP[2][MX][2];


int main() {
	if (fopen("in", "r")) freopen("in", "r", stdin), freopen("out", "w", stdout);
	ios_base::sync_with_stdio(0), cin.tie(0);

	int N; cin >> N;
	for (int i = 0; i < N; ++i) cin >> s[i];
	for (int i = 0; i < N; ++i) cin >> t[i];

	vector<ii> vc;
	for (int i = 0; i < N; ++i) vc.emplace_back(s[i], 0);
	for (int i = 0; i < N; ++i) vc.emplace_back(t[i], 1);
	sort(begin(vc), end(vc));

	DP[0][0][0] = 1;
	for (int i = 0; i < 2*N; ++i) {
		if (vc[i].s == 0) {
			for (int j = 0; j <= N; ++j) {
				(DP[1][j][1] += DP[0][j][0]) %= MOD;
				(DP[1][j+1][0] += DP[0][j][0]) %= MOD;
				(DP[1][j][1] += DP[0][j][1]) %= MOD;
				(DP[1][j+1][1] += DP[0][j][1]) %= MOD;
			}
		}
		else {
			for (int j = 0; j <= N; ++j) {
				(DP[1][j][0] += DP[0][j][0]) %= MOD;
				if (j) (DP[1][j-1][0] += j*DP[0][j][0]) %= MOD;
				if (j) (DP[1][j-1][1] += j*DP[0][j][1]) %= MOD;
			}
		}
		memcpy(DP[0], DP[1], sizeof DP[1]);
		memset(DP[1], 0, sizeof DP[1]);
	}
	cout << (DP[0][0][0]+DP[0][0][1])%MOD;
}