time limit per test : 2 seconds
memory limit per test : 256 megabytes
A permutation of size is an array of size such that each integer from to occurs exactly once in this array. An inversion in a permutation is a pair of indices such that and . For example, a permutation contains inversions: .
You are given a permutation of size . However, the numbers on some positions are replaced by . Let the valid permutation be such a replacement of in this sequence back to numbers from to in such a way that the resulting sequence is a permutation of size .
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
where
and Q are non-negative integers and Q≠0. Report the value of
(
).
Input
The first line contains a single integer
— the length of the sequence.The second line contains
integers
— the initial sequence.
It is guaranteed that all elements not equal to
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
where
and
are non-negative integers and
≠
. Report the value of
(
).
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:
inversions;
inversions.
The expected value is
.
In the second example no
are present, thus the only valid permutation is possible — the given one. It has
inversions.
In the third example there are two resulting valid permutations — one with
inversions and one with
inversion.
题意:
给一个长度为
的排列,有一些位置为
表示不确定,求这个序列的逆序对数的期望。
题解:
分成这几种情况讨论:
1:如果两个数字都是不确定的,那么显然对答案的贡献为
2:假设
且
且
,那么这个
对答案的贡献就是
表示在序列中a[j]的左边有几个不确定的位置,
表示不确定的数字中比
小的
3:假设
且
且
,那么这个
对答案的贡献就是
表示在序列中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;
}