CF2190E.Median Permutation
NOI/NOI+/CTSC
通过率:0%
时间限制:2.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
For a permutation∗ q of size m≥3, define f(q) to be a sequence b of size m−2 such that bi=med(qi,qi+1,qi+2) for all 1≤i≤m−2. Here, med(x,y,z) denotes the second smallest element among x,y,z.
You are given an array a of size n, where some elements may be 0. It is guaranteed that a contains the values 1 and n (that is, there exist indices i,j such that ai=1 and aj=n).
Find the number of permutations p of size n satisfying the following conditions:
- p is consistent with a: for all 1≤i≤n, if ai=0, then pi=ai.
- All elements of f(p) are distinct.
Since the answer can be large, print it modulo 998244353.
∗A permutation of length n is an array consisting of n distinct integers from 1 to n in arbitrary order. For example, [2,3,1,5,4] is a permutation, but [1,2,2] is not a permutation (2 appears twice in the array), and [1,3,4] is also not a permutation (n=3 but there is 4 in the array).
对于一个长度为 m≥3 的排列∗ q,定义 f(q) 为一个长度为 m−2 的序列 b,其中对所有 1≤i≤m−2,有 bi=med(qi,qi+1,qi+2)。此处,med(x,y,z) 表示集合 {x,y,z} 中第二小的元素。
给定一个长度为 n 的数组 a,其中某些元素可能为 0。保证 a 中包含数值 1 和 n(即存在下标 i,j,使得 ai=1 且 aj=n)。
请找出满足以下条件的长度为 n 的排列 p 的个数:
- p 与 a 一致:对所有 1≤i≤n,若 ai=0,则 pi=ai;
- f(p) 中的所有元素互不相同。
由于答案可能很大,请输出其对 998244353 取模的结果。
∗ 长度为 n 的排列是指由 1 到 n 中 n 个互不相同的整数以任意顺序组成的数组。例如,[2,3,1,5,4] 是一个排列,而 [1,2,2] 不是排列(数组中 2 出现了两次),[1,3,4] 也不是排列(此时 n=3,但数组中出现了 4)。
输入格式
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 (3≤n≤2⋅105) — the size of the array.
The second line contains n integers a1,a2,…,an (0≤ai≤n).
It is guaranteed that all non-zero elements of a are pairwise distinct. It is also guaranteed that a contains the values 1 and n.
It is guaranteed that the sum of n over all test cases does not exceed 2⋅105.
每个测试包含多个测试用例。第一行包含测试用例的数量 t(1≤t≤104)。随后是各测试用例的描述。
每个测试用例的第一行包含一个整数 n(3≤n≤2⋅105)—— 数组的大小。
第二行包含 n 个整数 a1,a2,…,an(0≤ai≤n)。
保证数组 a 中所有非零元素两两不同。同时保证 a 中包含数值 1 和 n。
保证所有测试用例的 n 值之和不超过 2⋅105。
输出格式
For each test case, print a single integer — the number of permutations p satisfying the conditions, modulo 998244353.
对于每个测试用例,输出一个整数——满足条件的排列 p 的数量,对 998244353 取模。
输入输出样例
输入#1
5 3 1 3 2 5 0 5 4 1 0 7 0 0 1 0 0 7 0 10 1 10 0 0 0 0 0 0 0 0 15 0 0 10 0 0 15 0 0 6 7 0 1 0 0 3
输出#1
1 0 10 1 4
说明/提示
In the first example, the only permutation consistent with the input is p=[1,3,2]. We have f(p)=[2], since med(1,3,2)=2. The elements of f(p) are distinct, so this permutation is valid. The answer is 1.
In the second example, there are two permutations consistent with the input: p=[3,5,4,1,2] and p=[2,5,4,1,3].
- For p=[3,5,4,1,2], f(p)=[4,4,2].
- For p=[2,5,4,1,3], f(p)=[4,4,3].
In both cases, the value 4 appears twice in f(p). Thus, there are no valid permutations, and the answer is 0.
在第一个例子中,唯一与输入一致的排列是 p=[1,3,2]。我们有 f(p)=[2],因为 med(1,3,2)=2。f(p) 的元素互不相同,因此该排列是合法的。答案为 1。
在第二个例子中,有两个与输入一致的排列:p=[3,5,4,1,2] 和 p=[2,5,4,1,3]。
- 对于 p=[3,5,4,1,2],有 f(p)=[4,4,2]。
- 对于 p=[2,5,4,1,3],有 f(p)=[4,4,3]。
在这两种情况下,值 4 在 f(p) 中均出现了两次。因此,不存在合法的排列,答案为 0。
输入解题思路,AI测评打分。不知道怎么写?