time limit per test : 2 seconds
memory limit per test : 256 megabytes
A permutation of size nnn is an array of size nnn such that each integer from 111 to nnn occurs exactly once in this array. An inversion in a permutation ppp is a pair of indices (i,j)(i,j)(i,j) such that i>ji>ji>j and ai<aja_i<a_jai<aj. For example, a permutation [4,1,3,2][4,1,3,2][4,1,3,2] contains 444 inversions: (2,1),(3,1),(4,1),(4,3)(2,1), (3,1), (4,1), (4,3)(2,1),(3,1),(4,1),(4,3).
You are given a permutation ppp of size nnn. However, the numbers on some positions are replaced by −1−1−1. Let the valid permutation be such a replacement of −1−1−1 in this sequence back to numbers from 111 to nnn in such a way that the resulting sequence is a permutation of size nnn.
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 P∗Q−1P*Q^{-1}P∗Q−1
where PPP and Q are non-negative integers and Q≠0. Report the value of P⋅Q−1P⋅Q^{−1}P⋅Q−1(modmodmod 998244353998244353998244353).
Input
The first line contains a single integer n(1≤n≤2⋅105)n(1≤n≤2⋅10^5)n(1≤n≤2⋅105) — the length of the sequence.The second line contains nnn integers p1,p2,…,pn(−1≤pi≤n,pi≠0)p_1,p_2,…,p_n (−1≤p_i≤n, pi≠0)p1,p2,…,pn(−1≤pi≤n,pi̸=0) — the initial sequence.
It is guaranteed that all elements not equal to −1−1−1 are pairwise distinct.
Output
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 P∗Q−1P*Q^{-1}P∗Q−1
where PPP and QQQ are non-negative integers and QQQ ≠ 000. Report the value of P⋅Q−1P⋅Q^{−1}P⋅Q−1(modmodmod 998244353998244353998244353).
Examples
Input
3
3 -1 -1
Output
499122179
Input
2
1 2
Output
0
Input
2
-1 -1
Output
499122177
Note
In the first example two resulting valid permutations are possible:
[3,1,2]—2[3,1,2]— 2[3,1,2]—2 inversions;
[3,2,1]—3[3,2,1]— 3[3,2,1]—3 inversions.
The expected value is 2⋅1+3⋅(1/2)=2.52⋅1+3⋅(1/2)=2.52⋅1+3⋅(1/2)=2.5.
In the second example no −1−1−1 are present, thus the only valid permutation is possible — the given one. It has 000 inversions.
In the third example there are two resulting valid permutations — one with 000 inversions and one with 111 inversion.
题意:
给一个长度为nnn的排列,有一些位置为−1-1−1表示不确定,求这个序列的逆序对数的期望。
题解:
分成这几种情况讨论:
1:如果两个数字都是不确定的,那么显然对答案的贡献为1/21/21/2
2:假设i<ji<ji<j且a[i]==−1a[i]==-1a[i]==−1且a[j]!=−1a[j]!=-1a[j]!=−1,那么这个a[j]a[j]a[j]对答案的贡献就是xj∗sumj/sumUx_j*sum_j/sumUxj∗sumj/sumU
xjx_jxj表示在序列中a[j]的左边有几个不确定的位置,sumjsum_jsumj表示不确定的数字中比a[j]a[j]a[j]小的
3:假设i>ji>ji>j且a[i]==−1a[i]==-1a[i]==−1且a[j]!=−1a[j]!=-1a[j]!=−1,那么这个a[j]a[j]a[j]对答案的贡献就是xj∗sumtj/sumUx_j*sumt_j/sumUxj∗sumtj/sumU
xjx_jxj表示在序列中a[j]的左边有几个不确定的位置,sumtjsumt_jsumtj表示不确定的数字中比a[j]a[j]a[j]大的
4:如果两个数字都是确定的那就直接算就行。
对这4种情况分别计算即可。
#include<bits/stdc++.h>
#define LiangJiaJun main
#define MOD 998244353LL
#define ll long long
using namespace std;
inline int lowbit(int x){return x&(-x);}
ll fp(ll x,ll y){
if(y==0)return 1;
ll temp=fp(x,y>>1);
if(y&1){
return (((temp*temp)%MOD)*x)%MOD;
}
else return (temp*temp)%MOD;
}
ll rev(ll x){
return fp(x,MOD-2);
}
int n,a[200004];
int tr[200004],c[200004];
ll rev4,revc,ans,sum;
void add(int x){
for(int i=x;i<=n;i+=lowbit(i))tr[i]++;
}
int query(int x){
int res=0;
for(int i=x;i;i-=lowbit(i))res+=tr[i];
return res;
}
int goc(int x){
return x-query(x);
}
int LiangJiaJun(){
scanf("%d",&n);
c[0]=0;
sum=0;
ans=0;
for(int i=1;i<=n;i++){
scanf("%d",&a[i]);
sum+=(a[i]!=-1);
if(a[i]!=-1)add(a[i]);
c[i]=c[i-1]+(a[i]==-1);
}
rev4=rev(4);
revc=rev(c[n]);
ans=((1LL*(c[n])*(c[n]-1))%MOD);
ans=(ans*rev4)%MOD;
for(int i=1;i<=n;i++){
if(a[i]!=-1){
ll now=n-sum-goc(a[i]);
now=(now*c[i])%MOD;
now=(now*revc)%MOD;
ans=(ans+now)%MOD;
now=goc(a[i]);
now=(now*(c[n]-c[i]))%MOD;
now=(now*revc)%MOD;
ans=(ans+now)%MOD;
}
}
memset(tr,0,sizeof(tr));
int kac=0;
for(int i=1;i<=n;i++){
if(a[i]==-1)continue;
ans+=kac-query(a[i]);
kac++;
add(a[i]);
}
printf("%lld\n",(ans+MOD)%MOD);
return 0;
}