CF2249B.Permutation Cuts
普及+/提高
通过率:0%
时间限制:4.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
You are given an integer n and an array a of length n−1.
For a permutation∗ p of length n, define
v_i=minleft(max_1lejleip_j,max_i+1lejlenp_jright),
where 1≤i≤n−1.
In other words, cut the permutation between positions i and i+1, take the maximum element on each side, and let vi be the smaller of these two values.
Your task is to count the number of permutations p of length n such that vi=ai for each 1≤i≤n−1.
Output the answer 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).
给你一个整数 n 和一个长度为 n−1 的数组 a。
对于一个长度为 n 的排列∗ p,定义
v_i=minleft(max_1lejleip_j,max_i+1lejlenp_jright),
其中 1≤i≤n−1。
换言之,将排列在位置 i 与 i+1 之间切开,取左右两段各自的最大值,并令 vi 为这两个最大值中的较小者。
你的任务是计算满足对每个 1≤i≤n−1 均有 vi=ai 的长度为 n 的排列 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 one integer n (2≤n≤106) — the length of p.
The second line contains n−1 integers a1,a2,…,an−1 (1≤ai≤n) — the elements of a.
It is guaranteed that the sum of n over all test cases does not exceed 106.
每个测试包含多个测试用例。第一行包含测试用例的数量 t(1≤t≤104)。随后是测试用例的描述。
每个测试用例的第一行包含一个整数 n(2≤n≤106)——即 p 的长度。
第二行包含 n−1 个整数 a1,a2,…,an−1(1≤ai≤n)——即数组 a 的元素。
保证所有测试用例的 n 之和不超过 106。
输出格式
For each test case, output one integer — the number of suitable permutations, modulo 998244353.
对于每个测试用例,输出一个整数——满足条件的排列数目对 998244353 取模的结果。
输入输出样例
输入#1
11 2 1 3 2 2 3 1 1 4 2 3 2 5 3 3 4 2 2 2 3 1 2 4 3 3 3 5 4 4 4 4 4 2 1 2 6 3 3 5 5 5
输出#1
2 2 0 0 2 0 2 4 12 0 8
说明/提示
In the first test case, both permutations [1,2] and [2,1] satisfy v1=1.
In the second test case, the only suitable permutations are [2,1,3] and [3,1,2].
In the third test case, no suitable permutation exists.
在第一个测试用例中,排列 [1,2] 和 [2,1] 均满足 v1=1。
在第二个测试用例中,唯一满足条件的排列是 [2,1,3] 和 [3,1,2]。
在第三个测试用例中,不存在满足条件的排列。
输入解题思路,AI测评打分。不知道怎么写?