有一個 的排列 。求有多少個排列 st 每一個 ,有 。
令 表示 在排列 中的位置。考慮列舉 的值。如果 則 一定包含了 的所有值,因此這個 , 應當滿足 且 。同時,為了保證 的 與 相同,對於值 ,如果 ,則在 中, 可以放在 的任意一個位置,否則必須放在與 相同的位置。
void solve()
{
int n;
scanf("%d", &n);
std::vector<int> a(n);
for (auto &i : a) scanf("%d", &i);
std::vector<int> pos(n);
for (int i = 0; i < n; i++)
pos[a[i]] = i;
int l = pos[0], r = pos[0];
long long ans = 1;
for (int i = 1; i < n; i++)
{
if (l <= pos[i] && pos[i] <= r) ans = ans * (r - l + 1 - i) % MODN; // 注意減去已經安排好位置的 i 個元素.
l = std::min(l, pos[i]);
r = std::max(r, pos[i]);
}
printf("%lld\n", ans);
}