Ex - Group Photo Editorial by en_translator
We assume that \(A\) and \(B\) are sorted.
First, we define a sequence \(c = (c_1,c_2,\dots,c_{N+1})\) as follows.
- \(c_i=\min\{a_{i-1},a_i\}\ (2 \leq i \leq N)\)
- \(c_1=a_1\)
- \(c_{N+1}=a_N\)
Then, an arrangement is good if and only if \(b_i > c_i\) for all \(i\ (1 \leq i \leq N+1)\). Thus, for an array \(C\) obtained by sorting \(C\), one can rearrange the back row to make the arrangement good if and only if \(B_i > C_i\) for all \(i\ (1 \leq i \leq N+1)\).
Consider an insertion DP (Dynamic Programming) to determine the relative ordering between \(A_1,A_2,\dots,A_N\). Here, for each adjacent elements when the ordering of \(A_1,A_2,\dots,A_i\) is determine, we distinguish whether the pair ends up being adjacent or any element after \(A_{i+1}\) is coming in. Also, we manage the number of connected components when an edge is added between each pair of elements that ends up being adjacent as the second key of the DP. In an transition (when inserting \(A_{i+1}\)), we need to consider the following three patterns:
- \(A_{i+1}\) forms a new independent connected component;
- \(A_{i+1}\) sticks onto the left or right of an existing connected component;
- \(A_{i+1}\) glues two existing connected components to reduce the number of connected components by one.
In any case, the coefficients of the transitions can be expressed in a simple form.
Finally, we assert that we only need to store the number of connected components to determine if rearranging the back row can result in a good arrangement.
From the information that there are \(j\) connected components within \(A_1,A_2,\dots,A_i\), we can identify that \(C\) has \((i+1)\) elements less than or equal to \(A_i\), which is sufficient to determine the conditions. Specifically, when transitioning from “elements up to \(A_i\) have been placed, and \(C\) has \(k\) elements no more than \(A_i\)” to “elements up to \(A_{i+1}\) have been placed, and \(C\) has \(l\) elements no more than \(A_{i+1}\)”, we can check if all of \(B_{k+1},B_{k+2},\dots,B_l\) is no less than \(A_{i+1}\).
Therefore, the problem has been solved in a total of \(O(N^2)\) time. See also the figure below and the sample code.
Description of the figure: the DP transition from top to bottom. \(i\) is the number of elements placed so far, \(j\) is the number of connected components, and \(k\ (=i+j)\) is the number of elements in \(C\) that is no more than \(A_i\). The V mark denotes the positions of \(k\) elements no more than \(A_i\) in \(C\) (where the V mark between \(a_p\) and \(a_{p+1}\) denotes \(C_{p+1}\)).
Sample code (C++):
#include<bits/stdc++.h>
#include<atcoder/modint>
using namespace std;
using namespace atcoder;
using mint = modint998244353;
int main() {
int n;
cin >> n;
vector<int> a(n), b(n + 1);
for (int &i: a) cin >> i;
for (int &i: b) cin >> i;
sort(a.begin(), a.end());
sort(b.begin(), b.end());
vector dp(n + 1, vector<mint>(n + 1));
dp[0][0] = 1;
for (int i = 0; i < n; i++) {
for (int j = 0; j <= i; j++) {
for (int p = 0; p < 3; p++) {
if (p > j) break;
if (i + j + 2 - p > n + 1) continue;
if (p <= 1 and b[i + j] < a[i]) continue;
mint coef;
if (p == 0) coef = j + 1;
else if (p == 1) coef = 2 * j;
else coef = j - 1;
dp[i + 1][j + 1 - p] += dp[i][j] * coef;
}
}
}
cout << dp[n][1].val() << endl;
}
posted:
last update: