CF1909F2.Small Permutation Problem (Hard Version)
省选/NOI-
通过率:0%
时间限制:2.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
⠀
In the easy version, the ai are in the range [0,n]; in the hard version, the ai are in the range [−1,n] and the definition of good permutation is slightly different. You can make hacks only if all versions of the problem are solved.
You are given an integer n and an array a1,a2,…,an of integers in the range [−1,n].
A permutation p1,p2,…,pn of [1,2,…,n] is good if, for each i, the following condition is true:
- if ai=−1, the number of values ≤i in [p1,p2,…,pi] is exactly ai.
Count the good permutations of [1,2,…,n], modulo 998244353.
⠀
在简单版本中,ai 的取值范围为 [0,n];在困难版本中,ai 的取值范围为 [−1,n],且“好排列”的定义略有不同。仅当本题所有版本均已被解决时,才允许进行 Hack。
给定一个整数 n 和一个长度为 n 的整数数组 a1,a2,…,an,其中每个 ai∈[−1,n]。
排列 p1,p2,…,pn 是 [1,2,…,n] 的一个好排列,当且仅当对每个 i,以下条件成立:
- 若 ai=−1,则子数组 [p1,p2,…,pi] 中不大于 i 的元素个数恰好为 ai。
求 [1,2,…,n] 的好排列个数,对 998244353 取模。
输入格式
Each test contains multiple test cases. The first line contains the number of test cases t (1≤t≤104). The description of the test cases follows.
The first line of each test case contains a single integer n (1≤n≤2⋅105) — the length of the array a.
The second line of each test case contains n integers a1,a2,…,an (−1≤ai≤n), which describe the conditions for a good permutation.
It is guaranteed that the sum of n over all test cases does not exceed 2⋅105.
每个测试包含多个测试用例。第一行包含测试用例的数量 t(1≤t≤104)。随后是各测试用例的描述。
每个测试用例的第一行包含一个整数 n(1≤n≤2⋅105)—— 表示数组 a 的长度。
每个测试用例的第二行包含 n 个整数 a1,a2,…,an(−1≤ai≤n),用于描述“好排列”的条件。
保证所有测试用例的 n 值之和不超过 2⋅105。
输出格式
For each test case, output a single line containing the number of good permutations, modulo 998244353.
对于每个测试用例,输出一行,包含好排列的数量对 998244353 取模的结果。
输入输出样例
输入#1
10 5 -1 -1 -1 -1 -1 5 1 2 3 4 5 6 0 2 2 2 -1 -1 6 -1 -1 -1 -1 -1 5 6 -1 -1 3 2 -1 -1 15 0 0 -1 -1 -1 2 2 -1 -1 -1 -1 9 11 13 15 6 0 2 2 2 4 6 6 0 1 3 4 5 5 6 1 2 3 2 4 6 15 0 0 1 1 1 2 3 4 5 6 7 9 11 13 15
输出#1
120 1 4 0 0 494403526 4 0 0 532305727
说明/提示
In the first test case, all the permutations of length 5 are good, so there are 120 good permutations.
In the second test case, the only good permutation is [1,2,3,4,5].
In the third test case, there are 4 good permutations: [2,1,5,6,3,4], [2,1,5,6,4,3], [2,1,6,5,3,4], [2,1,6,5,4,3]. For example, [2,1,5,6,3,4] is good because:
- a1=0, and there are 0 values ≤1 in [p1]=[2];
- a2=2, and there are 2 values ≤2 in [p1,p2]=[2,1];
- a3=2, and there are 2 values ≤3 in [p1,p2,p3]=[2,1,5];
- a4=2, and there are 2 values ≤4 in [p1,p2,p3,p4]=[2,1,5,6];
- a5=−1, so there are no restrictions on [p1,p2,p3,p4,p5];
- a6=−1, so there are no restrictions on [p1,p2,p3,p4,p5,p6].
在第一个测试用例中,所有长度为 5 的排列都是合法的,因此共有 120 个合法排列。
在第二个测试用例中,唯一的合法排列是 [1,2,3,4,5]。
在第三个测试用例中,共有 4 个合法排列:[2,1,5,6,3,4]、[2,1,5,6,4,3]、[2,1,6,5,3,4]、[2,1,6,5,4,3]。例如,[2,1,5,6,3,4] 是合法的,因为:
- a1=0,且在 [p1]=[2] 中 ≤1 的值有 0 个;
- a2=2,且在 [p1,p2]=[2,1] 中 ≤2 的值有 2 个;
- a3=2,且在 [p1,p2,p3]=[2,1,5] 中 ≤3 的值有 2 个;
- a4=2,且在 [p1,p2,p3,p4]=[2,1,5,6] 中 ≤4 的值有 2 个;
- a5=−1,因此对 [p1,p2,p3,p4,p5] 没有限制;
- a6=−1,因此对 [p1,p2,p3,p4,p5,p6] 没有限制。
输入解题思路,AI测评打分。不知道怎么写?