CF1096.F. Inversion Expectation(树状数组)
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
3
3 -1 -1
499122179
2
1 2
0
2
-1 -1
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(树状数组)的更多相关文章
- HDU 1394 Minimum Inversion Number ( 树状数组求逆序数 )
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1394 Minimum Inversion Number ...
- [hdu1394]Minimum Inversion Number(树状数组)
Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java ...
- 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 ...
- 牛客网暑期ACM多校训练营(第五场) F - take —— 期望+树状数组+逆元
看到一篇好的博客特意转出来观摩大佬:转:https://blog.csdn.net/greybtfly/article/details/81413526 题目大意:给n个箱子排成一排,从头到尾按顺序依 ...
- HDU 1394 Minimum Inversion Number (树状数组求逆序对)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1394 题目让你求一个数组,这个数组可以不断把最前面的元素移到最后,让你求其中某个数组中的逆序对最小是多 ...
- HDU 1394 Minimum Inversion Number (树状数组 && 规律 && 逆序数)
题意 : 有一个n个数的数列且元素都是0~n-1,问你将数列的其中某一个数及其前面的数全部置到后面这种操作中(比如3 2 1 0中选择第二个数倒置就产生1 0 3 2)能产生的最少的逆序数对是多少? ...
- 2019牛客多校第七场 F Energy stones 树状数组+算贡献转化模拟
Energy stones 题意 有n块石头,每块有初始能量E[i],每秒石头会增长能量L[i],石头的能量上限是C[i],现有m次时刻,每次会把[s[i],t[i]]的石头的能量吸干,问最后得到了多 ...
- hdu 1394 Minimum Inversion Number - 树状数组
The inversion number of a given number sequence a1, a2, ..., an is the number of pairs (ai, aj) that ...
- F - Moving Points树状数组
题:https://codeforces.com/contest/1311/problem/F 题意:给定x轴上的点以及他们的速度v,只在x轴上运动,求最小的dis之和,注意,这里的时间是可随意的,比 ...
随机推荐
- [原][OSG]深入osg函数----SceneView::cull 函数
参考:最长一帧 先介绍几个类: osgUtil::CullVisitor:“筛选访问器” 当我们使用它遍历场景图形的各个节点时, CullVisitor 将会对每一个遇到的节点执行场景筛选的工作,判断 ...
- box-shadow四周都有阴影
<style> .shadow{ -webkit-box-shadow: #666 0px 0px 10px; -moz-box-shadow: #666 0px 0px 10px; bo ...
- Redis之有序集合命令
Redis 有序集合(sorted set) Redis 有序集合和集合一样也是string类型元素的集合,且不允许重复的成员. 不同的是每个元素都会关联一个double类型的分数.redis正是通过 ...
- centos7: svbversion版本的安装配置+tortoisesvn登录验证
centos7: svbversion版本的安装配置+tortoisesvn登录验证 命令工具:svnadmin create #创建版本库 hotcopy #版本库热备份 Islocks #打印所有 ...
- Jersey 2.x JDK 上的客户端应用
如应用是运行在 JDK 上的话,你只需要使用 JAX-RS 中的客户端部分就可以了,这个根据你使用的客户端有所调整. 这里有一系列的模块是可以供你使用的,例如 grizzly 或 apache 或 j ...
- python-day5笔记
一.python基础--基本数据类型 (无论用户输入什么内容,input 都会存成字符串格式) 1.基本数据类型 1)数字类型 整型(整数)int:年级,年纪,等级,身份证号,QQ号,手机号,leve ...
- 关于二级指针的使用(使用node指针建树)
struct node { int v; node *l,*r; }*p; 使用二级指针建树的话,如果p是非全局变量且一开始没有指向变量的话递归建树时必然要传递参数,但是如果只是简单的build(no ...
- UVA-10689 Yet another Number Sequence (矩阵二分幂模板)
题目大意:已知递推公式和边缘值,求某项的最后m(0<m<5)位数字. 题目分析:矩阵二分幂的模板题. 代码如下: # include<iostream> # include&l ...
- 安装 Android Studio
安装 Android Studio 只需轻松点击几下.(您需要已下载 Android Studio.) 若使用 JDK 1.8,在 Mac 系统上运行 Android Studio 可能出现一些已知的 ...
- 使用UTL_SMTP发送中文邮件及使用UTL_TCP从附件服务器获取中文附件
先上最重要的干货 发送邮件正文及主题的时候一定要使用convert重新编码 主题: utl_smtp.write_raw_data(l_mail_conn, utl_raw.cast_to_raw(c ...