CF1096F.Inversion Expectation

提高+/省选-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

A permutation of size nn is an array of size nn such that each integer from 11 to nn occurs exactly once in this array. An inversion in a permutation pp is a pair of indices (i,j)(i, j) such that i>ji \gt j and ai<aja_i \lt a_j. For example, a permutation [4,1,3,2][4, 1, 3, 2] contains 44 inversions: (2,1)(2, 1), (3,1)(3, 1), (4,1)(4, 1), (4,3)(4, 3).

You are given a permutation pp of size nn. However, the numbers on some positions are replaced by −1-1. Let the valid permutation be such a replacement of −1-1 in this sequence back to numbers from 11 to nn in such a way that the resulting sequence is a permutation of size nn.

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 PQ\frac{P}{Q} where PP and QQ are non-negative integers and Q≠0Q \ne 0. Report the value of P⋅Q−1(mod998244353)P \cdot Q^{-1} \pmod {998244353}.

大小为 nn 的一个排列是指一个长度为 nn 的数组,其中每个从 11 到 nn 的整数恰好出现一次。在排列 pp 中,一个逆序对(inversion)是指一对下标 (i,j)(i, j),满足 i>ji > j 且 ai<aja_i < a_j。例如,排列 [4,1,3,2][4, 1, 3, 2] 包含 44 个逆序对:(2,1)(2, 1)、(3,1)(3, 1)、(4,1)(4, 1)、(4,3)(4, 3)。

你被给定一个大小为 nn 的排列 pp,但其中某些位置上的数字被替换成了 −1-1。我们称一个合法排列(valid permutation)为:将该序列中所有 −1-1 替换为 11 到 nn 中的某些数(每个数至多使用一次),使得最终得到的序列是一个大小为 nn 的排列。

已知该给定序列是通过等概率随机地选择所有可能的合法排列之一而得到的。

请计算所得合法排列中逆序对总数的期望值。

可以证明该期望值可表示为 PQ\frac{P}{Q} 的形式,其中 PP 和 QQ 是非负整数且 Q≠0Q \ne 0。请输出 P⋅Q−1 mod 998244353P \cdot Q^{-1} \bmod 998244353 的值。

输入格式

The first line contains a single integer nn (1≤n≤2⋅1051 \le n \le 2 \cdot 10^5) — the length of the sequence.

The second line contains nn integers p1,p2,…,pnp_1, p_2, \dots, p_n (−1≤pi≤n-1 \le p_i \le n, pi≠0p_i \ne 0) — the initial sequence.

It is guaranteed that all elements not equal to −1-1 are pairwise distinct.

第一行包含一个整数 nn(1≤n≤2⋅1051 \le n \le 2 \cdot 10^5)—— 序列的长度。

第二行包含 nn 个整数 p1,p2,…,pnp_1, p_2, \dots, p_n(−1≤pi≤n-1 \le p_i \le n,且 pi≠0p_i \ne 0)—— 初始序列。

保证所有不等于 −1-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 PQ\frac{P}{Q} where PP and QQ are non-negative integers and Q≠0Q \ne 0. Report the value of P⋅Q−1(mod998244353)P \cdot Q^{-1} \pmod {998244353}.

输出一个整数——结果中合法排列的逆序对总数的期望值。

可以证明该期望值可表示为 PQ\frac{P}{Q} 的形式,其中 PP 和 QQ 为非负整数且 Q≠0Q \ne 0。请输出 P⋅Q−1(mod998244353)P \cdot Q^{-1} \pmod {998244353} 的值。

输入输出样例

  • 输入#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][3, 1, 2] — 22 inversions;
  • [3,2,1][3, 2, 1] — 33 inversions.

The expected value is 2⋅1+3⋅12=2.5\frac{2 \cdot 1 + 3 \cdot 1}{2} = 2.5.

In the second example no −1-1 are present, thus the only valid permutation is possible — the given one. It has 00 inversions.

In the third example there are two resulting valid permutations — one with 00 inversions and one with 11 inversion.

在第一个例子中,存在两个可能的合法排列:

  • [3,1,2][3, 1, 2] — 含 22 个逆序对;
  • [3,2,1][3, 2, 1] — 含 33 个逆序对。

期望值为 2⋅1+3⋅12=2.5\frac{2 \cdot 1 + 3 \cdot 1}{2} = 2.5。

在第二个例子中,输入中不含 −1-1,因此唯一可能的合法排列即为给定排列。该排列含有 00 个逆序对。

在第三个例子中,存在两个可能的合法排列——其中一个含 00 个逆序对,另一个含 11 个逆序对。

输入解题思路,AI测评打分。不知道怎么写?

首页