CF1096F.Inversion Expectation
提高+/省选-
通过率:0%
时间限制:2.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
A permutation of size n is an array of size n such that each integer from 1 to n occurs exactly once in this array. An inversion in a permutation p is a pair of indices (i,j) such that i>j and ai<aj. For example, a permutation [4,1,3,2] contains 4 inversions: (2,1), (3,1), (4,1), (4,3).
You are given a permutation p of size n. However, the numbers on some positions are replaced by −1. Let the valid permutation be such a replacement of −1 in this sequence back to numbers from 1 to n in such a way that the resulting sequence is a permutation of size n.
The given sequence was turned into a valid permutation randomly with the equal probability of getting each valid permutation.
Calculate the expected total number of inversions in the resulting valid permutation.
It can be shown that it is in the form of QP where P and Q are non-negative integers and Q=0. Report the value of P⋅Q−1(mod998244353).
大小为 n 的一个排列是指一个长度为 n 的数组,其中每个从 1 到 n 的整数恰好出现一次。在排列 p 中,一个逆序对(inversion)是指一对下标 (i,j),满足 i>j 且 ai<aj。例如,排列 [4,1,3,2] 包含 4 个逆序对:(2,1)、(3,1)、(4,1)、(4,3)。
你被给定一个大小为 n 的排列 p,但其中某些位置上的数字被替换成了 −1。我们称一个合法排列(valid permutation)为:将该序列中所有 −1 替换为 1 到 n 中的某些数(每个数至多使用一次),使得最终得到的序列是一个大小为 n 的排列。
已知该给定序列是通过等概率随机地选择所有可能的合法排列之一而得到的。
请计算所得合法排列中逆序对总数的期望值。
可以证明该期望值可表示为 QP 的形式,其中 P 和 Q 是非负整数且 Q=0。请输出 P⋅Q−1mod998244353 的值。
输入格式
The first line contains a single integer n (1≤n≤2⋅105) — the length of the sequence.
The second line contains n integers p1,p2,…,pn (−1≤pi≤n, pi=0) — the initial sequence.
It is guaranteed that all elements not equal to −1 are pairwise distinct.
第一行包含一个整数 n(1≤n≤2⋅105)—— 序列的长度。
第二行包含 n 个整数 p1,p2,…,pn(−1≤pi≤n,且 pi=0)—— 初始序列。
保证所有不等于 −1 的元素两两互不相同。
输出格式
Print a single integer — the expected total number of inversions in the resulting valid permutation.
It can be shown that it is in the form of QP where P and Q are non-negative integers and Q=0. Report the value of P⋅Q−1(mod998244353).
输出一个整数——结果中合法排列的逆序对总数的期望值。
可以证明该期望值可表示为 QP 的形式,其中 P 和 Q 为非负整数且 Q=0。请输出 P⋅Q−1(mod998244353) 的值。
输入输出样例
输入#1
3 3 -1 -1
输出#1
499122179
输入#2
2 1 2
输出#2
0
输入#3
2 -1 -1
输出#3
499122177
说明/提示
In the first example two resulting valid permutations are possible:
- [3,1,2] — 2 inversions;
- [3,2,1] — 3 inversions.
The expected value is 22⋅1+3⋅1=2.5.
In the second example no −1 are present, thus the only valid permutation is possible — the given one. It has 0 inversions.
In the third example there are two resulting valid permutations — one with 0 inversions and one with 1 inversion.
在第一个例子中,存在两个可能的合法排列:
- [3,1,2] — 含 2 个逆序对;
- [3,2,1] — 含 3 个逆序对。
期望值为 22⋅1+3⋅1=2.5。
在第二个例子中,输入中不含 −1,因此唯一可能的合法排列即为给定排列。该排列含有 0 个逆序对。
在第三个例子中,存在两个可能的合法排列——其中一个含 0 个逆序对,另一个含 1 个逆序对。
输入解题思路,AI测评打分。不知道怎么写?