[codeforces1096F]Inversion Expectation

版权声明:辛辛苦苦码字,你们转载的时候记得告诉我 https://blog.csdn.net/dxyinme/article/details/90744751

time limit per test : 2 seconds
memory limit per test : 256 megabytes

A permutation of size n n is an array of size n n such that each integer from 1 1 to n n occurs exactly once in this array. An inversion in a permutation p p is a pair of indices ( i , j ) (i,j) such that i > j i>j and a i < a j a_i<a_j . For example, a permutation [ 4 , 1 , 3 , 2 ] [4,1,3,2] contains 4 4 inversions: ( 2 , 1 ) , ( 3 , 1 ) , ( 4 , 1 ) , ( 4 , 3 ) (2,1), (3,1), (4,1), (4,3) .

You are given a permutation p p of size n n . 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 1 1 to n n in such a way that the resulting sequence is a permutation of size n 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 P Q 1 P*Q^{-1}
where P P and Q are non-negative integers and Q≠0. Report the value of P Q 1 P⋅Q^{−1} ( m o d mod 998244353 998244353 ).

Input

The first line contains a single integer n ( 1 n 2 1 0 5 ) n(1≤n≤2⋅10^5) — the length of the sequence.The second line contains n n integers p 1 , p 2 , , p n ( 1 p i n , p i 0 ) p_1,p_2,…,p_n (−1≤p_i≤n, pi≠0) — the initial sequence.
It is guaranteed that all elements not equal to 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 1 P*Q^{-1}
where P P and Q Q are non-negative integers and Q Q 0 0 . Report the value of P Q 1 P⋅Q^{−1} ( m o d mod 998244353 998244353 ).
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 inversions;
[ 3 , 2 , 1 ] 3 [3,2,1]— 3 inversions.
The expected value is 2 1 + 3 ( 1 / 2 ) = 2.5 2⋅1+3⋅(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 0 0 inversions.
In the third example there are two resulting valid permutations — one with 0 0 inversions and one with 1 1 inversion.

题意:
给一个长度为 n n 的排列,有一些位置为 1 -1 表示不确定,求这个序列的逆序对数的期望。

题解:
分成这几种情况讨论:

1:如果两个数字都是不确定的,那么显然对答案的贡献为 1 / 2 1/2

2:假设 i < j i<j a [ i ] = = 1 a[i]==-1 a [ j ] ! = 1 a[j]!=-1 ,那么这个 a [ j ] a[j] 对答案的贡献就是 x j s u m j / s u m U x_j*sum_j/sumU
x j x_j 表示在序列中a[j]的左边有几个不确定的位置, s u m j sum_j 表示不确定的数字中比 a [ j ] a[j] 小的

3:假设 i > j i>j a [ i ] = = 1 a[i]==-1 a [ j ] ! = 1 a[j]!=-1 ,那么这个 a [ j ] a[j] 对答案的贡献就是 x j s u m t j / s u m U x_j*sumt_j/sumU
x j x_j 表示在序列中a[j]的左边有几个不确定的位置, s u m t j sumt_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;
}

猜你喜欢

转载自blog.csdn.net/dxyinme/article/details/90744751