[codeforces1096F]Inversion Expectation

本文介绍了一种算法,用于计算给定序列中不确定位置(标记为-1)的逆序对数的期望值。该算法考虑了四种情况,包括两个不确定数字、一个确定和一个不确定数字的不同组合,以及两个确定数字的直接计算。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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&gt;ji&gt;ji>j and ai&lt;aja_i&lt;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−11. Let the valid permutation be such a replacement of −1−11 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}PQ1
where PPP and Q are non-negative integers and Q≠0. Report the value of P⋅Q−1P⋅Q^{−1}PQ1(modmodmod 998244353998244353998244353).

Input

The first line contains a single integer n(1≤n≤2⋅105)n(1≤n≤2⋅10^5)n(1n2105) — 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(1pin,pi̸=0) — the initial sequence.
It is guaranteed that all elements not equal to −1−11 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}PQ1
where PPP and QQQ are non-negative integers and QQQ000. Report the value of P⋅Q−1P⋅Q^{−1}PQ1(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.521+3(1/2)=2.5.
In the second example no −1−11 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-11表示不确定,求这个序列的逆序对数的期望。

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

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

2:假设i&lt;ji&lt;ji<ja[i]==−1a[i]==-1a[i]==1a[j]!=−1a[j]!=-1a[j]!=1,那么这个a[j]a[j]a[j]对答案的贡献就是xj∗sumj/sumUx_j*sum_j/sumUxjsumj/sumU
xjx_jxj表示在序列中a[j]的左边有几个不确定的位置,sumjsum_jsumj表示不确定的数字中比a[j]a[j]a[j]小的

3:假设i&gt;ji&gt;ji>ja[i]==−1a[i]==-1a[i]==1a[j]!=−1a[j]!=-1a[j]!=1,那么这个a[j]a[j]a[j]对答案的贡献就是xj∗sumtj/sumUx_j*sumt_j/sumUxjsumtj/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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值