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

.

You are given a permutation p

of size n. However, the numbers on some positions are replaced by −1. Let the valid permutation be such a replacement of −1 in this sequence back to numbers from 1 to n in such a way that the resulting sequence is a permutation of size 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 PQ

where P and Q are non-negative integers and Q≠0. Report the value of P⋅Q−1(mod998244353)

.

Input

The first line contains a single integer n

(1≤n≤2⋅105

) — the length of the sequence.

The second line contains n

integers p1,p2,…,pn (−1≤pi≤n, pi≠0

) — the initial sequence.

It is guaranteed that all elements not equal to −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 PQ

where P and Q are non-negative integers and Q≠0. Report the value of P⋅Q−1(mod998244353)

.

Examples

Input
3
3 -1 -1
Output
499122179
Input
2
1 2
Output
0
Input
2
-1 -1
Output
499122177

题意:给定一个数组,是一个N的排列,其中有些未知没有填数,让你补全,问逆序对的期望是多少。

思路:就是枚举几种情况就好了。

#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define ll long long
using namespace std;
const int maxn=;
const int Mod=;
int vis[maxn],a[maxn],b[maxn],cnt,sum[maxn],fac[maxn],ans,v,tot,N;
int qpow(int a,int x){
int res=; while(x){
if(x&) res=1LL*res*a%Mod;
a=1LL*a*a%Mod; x>>=;
} return res;
}
void add(int x){ for(;x<=N;x+=(-x)&x) sum[x]++;}
int query(int x){ int res=; for(;x;x-=(-x)&x) res+=sum[x]; return res; }
int main()
{
scanf("%d",&N);
rep(i,,N) {
scanf("%d",&a[i]);
if(a[i]!=-) vis[a[i]]=;
}
rep(i,,N) if(!vis[i]) b[++cnt]=i; //空位
sort(b+,b+cnt+);fac[]=; rep(i,,cnt) fac[i]=1LL*fac[i-]*i%Mod;
rep(i,,N){
if(a[i]!=-){
int Less=query(a[i]);
ans=(ans+1LL*(tot-Less)*fac[cnt]%Mod)%Mod; //已知+已知
int pos=lower_bound(b+,b+cnt+,a[i])-b; pos--;
ans=(ans+1LL*pos*(cnt-v)%Mod*fac[cnt-]%Mod)%Mod; //已知+未知
ans=(ans+1LL*(cnt-pos)*v%Mod*fac[cnt-]%Mod)%Mod;//未知+已知
tot++; add(a[i]);
}
else v++;
}
ans=(ans+1LL*cnt*(cnt-)%Mod*fac[cnt]%Mod*qpow(,Mod-)%Mod)%Mod; //未知+未知
fac[cnt]=qpow(fac[cnt],Mod-);
printf("%d\n",1LL*ans*fac[cnt]%Mod);
return ;
}

CF1096.F. Inversion Expectation(树状数组)的更多相关文章

  1. HDU 1394 Minimum Inversion Number ( 树状数组求逆序数 )

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1394 Minimum Inversion Number                         ...

  2. [hdu1394]Minimum Inversion Number(树状数组)

    Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java ...

  3. 2016-2017 ACM-ICPC Southwestern European Regional Programming Contest (SWERC 2016) F dfs序+树状数组

    Performance ReviewEmployee performance reviews are a necessary evil in any company. In a performance ...

  4. 牛客网暑期ACM多校训练营(第五场) F - take —— 期望+树状数组+逆元

    看到一篇好的博客特意转出来观摩大佬:转:https://blog.csdn.net/greybtfly/article/details/81413526 题目大意:给n个箱子排成一排,从头到尾按顺序依 ...

  5. HDU 1394 Minimum Inversion Number (树状数组求逆序对)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1394 题目让你求一个数组,这个数组可以不断把最前面的元素移到最后,让你求其中某个数组中的逆序对最小是多 ...

  6. HDU 1394 Minimum Inversion Number (树状数组 && 规律 && 逆序数)

    题意 : 有一个n个数的数列且元素都是0~n-1,问你将数列的其中某一个数及其前面的数全部置到后面这种操作中(比如3 2 1 0中选择第二个数倒置就产生1 0 3 2)能产生的最少的逆序数对是多少? ...

  7. 2019牛客多校第七场 F Energy stones 树状数组+算贡献转化模拟

    Energy stones 题意 有n块石头,每块有初始能量E[i],每秒石头会增长能量L[i],石头的能量上限是C[i],现有m次时刻,每次会把[s[i],t[i]]的石头的能量吸干,问最后得到了多 ...

  8. hdu 1394 Minimum Inversion Number - 树状数组

    The inversion number of a given number sequence a1, a2, ..., an is the number of pairs (ai, aj) that ...

  9. F - Moving Points树状数组

    题:https://codeforces.com/contest/1311/problem/F 题意:给定x轴上的点以及他们的速度v,只在x轴上运动,求最小的dis之和,注意,这里的时间是可随意的,比 ...

随机推荐

  1. 牛客练习赛7 E 珂朵莉的数列

    珂朵莉的数列 思路: 树状数组+高精度 离散化不知道哪里写错了,一直wa,最后用二分写的离散化 哪位路过大神可以帮我看看原来的那个离散化错在哪里啊 通过代码: import java.math.Big ...

  2. Codeforces 832D - Misha, Grisha and Underground

    832D - Misha, Grisha and Underground 思路:lca,求两个最短路的公共长度.公共长度公式为(d(a,b)+d(b,c)-d(a,c))/2. 代码: #includ ...

  3. SQLServer创建用户、数据库、表、约束、存储过程、视图

    --创建登录账户和数据库用户 ' exec sp_grantdbaccess 'sysAdmin','aa' --给数据库用户赋权限 grant select,update,insert,delete ...

  4. 很实用且容易忘记的小命令 for Linux(更新中...)

    系统相关 # 系统安装日期 sudo tune2fs -l /dev/sda1 |grep create # 查看centos版本命令 rpm -q centos-release #查看centos版 ...

  5. 1月24日 ruby基础3部分 Numeric, Array已学。

    <div style="background:lightblue"> 第12章 数值类 12.1 数值的构成 Numeric-> Integer-> Fix ...

  6. android--------动画之进度条

    Android开发中在处理耗时工作的时候,例如:列表加载,大多数会有一个精度条加载的框,里面有一个像gif的图片在旋转一样. 效果图:   <!--     根标签为animation-list ...

  7. luogu P2408 不同子串个数

    考虑反向操作,去计算有多少组相同的子串,对于一组大小为k的极大相同子串的集合,ans-=k-1. 为了避免重复计算,需要一种有效的,有顺序的记录方案. 比如说,对于每一个相同组,按其起始点所在的位置排 ...

  8. python-day54--前端之js-DOM对象

    一.DOM对象 1.什么是HTML  DOM HTML  Document Object Model(文档对象模型---标签) 2.功能:定义了访问(查找)和操作HTML文档的标准方法 3.HTML ...

  9. ORACLE中使用DBMS_SQL获取动态SQL执行结果中的列名和值

    1.获取动态SQL中的列名及类型 DECLARE l_curid INTEGER; l_cnt NUMBER; l_desctab dbms_sql.desc_tab; l_sqltext ); BE ...

  10. 标准API使用小技巧

    以前我始终孜孜不倦的追求完全能满足需求的标准的API,但是在使用的过程中发现,有些API并不能完全完整的满足需求. 导致我一度很纠结. 最后想到一种方式,COPY一份标准的API,然后按照自己的需求改 ...