AT_arc219_b.Reverse Permutation
普及/提高-
通过率:0%
时间限制:2.00s
内存限制:1024MB
AC君温馨提醒
该题目为【atcoder】题库的题目,您提交的代码将被提交至atcoder进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
You are given an integer N and a permutation P=(P1,P2,…,PN) of (1,2,…,N).
For a permutation Q=(Q1,Q2,…,QN) of (1,2,…,N), let Q′=(Q1′,Q2′,…,QN′) be the lexicographically smallest permutation obtainable by performing the following operation exactly once:
- Choose a pair of integers (l,r) satisfying 1≤l≤r≤N, and reverse Ql,Ql+1,…,Qr. More precisely, replace Q with (Q1,Q2,…,Ql−1,Qr,Qr−1,…,Ql,Qr+1,Qr+2,…,QN).
Find the number, modulo 998244353, of permutations Q of (1,2,…,N) such that Q′=P.
You are given T test cases; solve each of them.
给你一个整数 N 和 (1,2,…,N) 的一个排列 P=(P1,P2,…,PN)。
对于 (1,2,…,N) 的任意一个排列 Q=(Q1,Q2,…,QN),定义 Q′=(Q1′,Q2′,…,QN′) 为对 Q 恰好执行一次如下操作后所能得到的字典序最小的排列:
- 选择一对满足 1≤l≤r≤N 的整数 (l,r),并将子段 Ql,Ql+1,…,Qr 翻转。更准确地说,将 Q 替换为
(Q1,Q2,…,Ql−1,Qr,Qr−1,…,Ql,Qr+1,Qr+2,…,QN)。
求满足 Q′=P 的排列 Q(即 (1,2,…,N) 的排列)的个数,结果对 998244353 取模。
你将收到 T 组测试数据;请分别求解每组。
输入格式
The input is given from Standard Input in the following format:
T
case1
case2
⋮
caseT
Each test case is given in the following format:
N
P1 P2 … PN
输入从标准输入中按以下格式给出:
T
case1
case2
⋮
caseT
每个测试用例按以下格式给出:
N
P1 P2 … PN
输出格式
Output the answers for the test cases in order, separated by newlines.
按顺序输出测试用例的答案,答案之间用换行符分隔。
输入输出样例
输入#1
4 3 1 3 2 1 1 4 4 3 2 1 6 1 2 6 4 5 3
输出#1
2 1 0 9
说明/提示
Sample 1 Explanation:
Consider the first test case.
For example, when Q=(2,3,1), choosing (l,r)=(1,3) gives (1,3,2). No permutation lexicographically smaller than (1,3,2) can be obtained, so we have Q′=(1,3,2). Thus, Q=(2,3,1) satisfies Q′=P.
There are two permutations Q such that Q′=P: Q=(2,3,1),(3,1,2).
Constraints
- 1≤T
- 1≤N≤5×105
- P is a permutation of (1,2,…,N).
- The sum of N over all test cases is at most 5×105.
- All input values are integers.
样例 1 解释:
考虑第一个测试用例。
例如,当 Q=(2,3,1) 时,选择 (l,r)=(1,3) 可得到 (1,3,2)。无法通过任何操作得到字典序比 (1,3,2) 更小的排列,因此有 Q′=(1,3,2)。于是,Q=(2,3,1) 满足 Q′=P。
满足 Q′=P 的排列 Q 共有两个:Q=(2,3,1), (3,1,2)。
约束条件
- 1≤T
- 1≤N≤5×105
- P 是 (1,2,…,N) 的一个排列。
- 所有测试用例的 N 之和不超过 5×105。
- 所有输入值均为整数。
输入解题思路,AI测评打分。不知道怎么写?