#P7154. [USACO20DEC] Sleeping Cows P

    ID: 8090 远端评测题 1000ms 256MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>动态规划 DP2020USACOO2优化组合数学

[USACO20DEC] Sleeping Cows P

Problem Description

Farmer John has NN cows of various sizes (1N30001 \le N \le 3000). He originally custom-built a stall for each cow, but now some cows have grown, so the original stall sizes are no longer sufficient. Specifically, FJ originally built NN stalls with sizes t1,t2,,tNt_1, t_2, \ldots, t_N, and the cows now have sizes s1,s2,,sNs_1, s_2, \ldots, s_N (1si,ti1091 \le s_i, t_i \le 10^9).

Every night, the cows find stalls to sleep in in some way. Cow ii can sleep in stall jj if and only if her size can fit into the stall (sitjs_i \le t_j). Each stall can have at most one cow sleeping in it.

We say a matching between cows and stalls is maximal if and only if every assigned cow can fit into her assigned stall, and for every cow that is not assigned a stall, she cannot fit into any unassigned empty stall.

Compute the number of maximal matchings modulo 109+710^9 + 7.

Input Format

The first line contains NN.

The second line contains NN space-separated integers s1,s2,,sNs_1, s_2, \ldots, s_N.

The third line contains NN space-separated integers t1,t2,,tNt_1, t_2, \ldots, t_N.

Output Format

Output the number of maximal matchings modulo 109+710^9 + 7.

4
1 2 3 4
1 2 2 3
9

Hint

Below are all nine maximal matchings. The ordered pair (i,j)(i, j) means cow ii is assigned to stall jj.

(1, 1), (2, 2), (3, 4)
(1, 1), (2, 3), (3, 4)
(1, 1), (2, 4)
(1, 2), (2, 3), (3, 4)
(1, 2), (2, 4)
(1, 3), (2, 2), (3, 4)
(1, 3), (2, 4)
(1, 4), (2, 2)
(1, 4), (2, 3)
  • In testdata 2-3, N8N \le 8.
  • In testdata 4-12, N50N \le 50.
  • In testdata 13-20, there are no additional constraints.

Problem provided by: Nick Wu.

Translated by ChatGPT 5