[agc23E]Inversions
description
给你\(n\)和\(\{a_i\}\),你需要求所有满足\(p_i\le a_i\)的\(1-n\)排列的逆序对个数之和模\(10^9+7\)。
\(n \le 2\times10^5\)
sol
首先考虑一下所有满足要求的排列总数。记\(cnt_i\)表示有多少个\(a_k\ge i\),从大到小填数,方案数就是$$S=\prod_{i=1}^ncnt_i-(n-i)$$
考虑枚举两个位置\(i,j\),计算满足\(p_i>p_j\)的排列数。分两种情况讨论:
\(a_i\le a_j\):这时候如果你让\(a_j=a_i\)的话方案数也是一样的,所以就强制把\(a_j\)改成\(a_i\),计算满足条件的排列数。因为两个位置本质上没有区别,所以\(p_i>p_j\)的排列数会恰好是合法排列数的一半。而强制把\(a_j\)改小这个操作相当于是把连续一段的\(cnt_i\)减一,可以用某种方式来维护一下。
\(a_i>a_j\):总排列数减不合法,相当于是要求\(p_i<p_j\)的方案数。无非是把上面的\(i,j\)互换了而已,计算方法还是一样的。
考虑一下怎么把\(O(n^2)\)的枚举优化到\(O(n\log n)\)。记\(D_i=\frac{cnt_j-1-(n-j)}{cnt_j-(n-j)}\)。那么枚举一对\(i,j\),它们的贡献是$$S\times\prod_{k=a_i+1}{a_j}D_k=S\times\frac{\prod_{k=1}{a_j}D_k}{\prod_{k=1}^{a_i}D_k}$$
树状数组以\(a_i\)为下标,维护\(\frac{1}{\prod_{k=1}^{a_i}D_k}\)的前缀和即可。
但是这样有问题。因为\(D_i\)可能为\(0\)。我们可以对每个位置,找到它前面出现的第一个\(0\),显然这个\(0\)之前的所有\(i\)都不会有贡献了,而在这个\(0\)之后到\(j\)这一段一定没有\(0\),所以可以维护不含\(0\)的\(D_i\)前缀积。
\(a_i>a_j\)同理,需要额外维护个数以便计算总方案减不合法。
code
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
int gi(){
int x=0,w=1;char ch=getchar();
while ((ch<'0'||ch>'9')&&ch!='-') ch=getchar();
if (ch=='-') w=0,ch=getchar();
while (ch>='0'&&ch<='9') x=(x<<3)+(x<<1)+ch-'0',ch=getchar();
return w?x:-x;
}
const int N = 2e5+5;
const int mod = 1e9+7;
int n,a[N],cnt[N],S=1,z[N],st[N],D[N],ID[N],c1[N],c2[N];
int fastpow(int a,int b){
int res=1;
while (b) {if (b&1) res=1ll*res*a%mod;a=1ll*a*a%mod;b>>=1;}
return res;
}
void mdf(int k,int v){while(k<=n)c1[k]=(c1[k]+v)%mod,++c2[k],k+=k&-k;}
int qry1(int k){int s=0;while(k)s=(s+c1[k])%mod,k^=k&-k;return s;}
int qry2(int k){int s=0;while(k)s+=c2[k],k^=k&-k;return s;}
int main(){
n=gi();
for (int i=1;i<=n;++i) ++cnt[a[i]=gi()];
for (int i=n;i>=1;--i) cnt[i]+=cnt[i+1];
for (int i=1;i<=n;++i){
cnt[i]-=n-i;
if (cnt[i]<=0) return puts("0"),0;
S=1ll*S*cnt[i]%mod;
}
st[0]=D[0]=1;
for (int i=1;i<=n;++i){
int x=1ll*(cnt[i]-1)*fastpow(cnt[i],mod-2)%mod;
if (!x) st[z[i]=z[i-1]+1]=i,D[i]=D[i-1];
else z[i]=z[i-1],D[i]=1ll*D[i-1]*x%mod;
ID[i]=fastpow(D[i],mod-2);
}
int inv2=(mod+1)>>1,ans=0;
for (int i=1;i<=n;++i){
ans=(ans+1ll*(qry1(a[i])-qry1(st[z[a[i]]]-1)+mod)*D[a[i]]%mod*S%mod*inv2)%mod;
mdf(a[i],ID[a[i]]);
}
memset(c1,0,sizeof(c1)),memset(c2,0,sizeof(c2));
for (int i=n;i;--i){
ans=(ans-1ll*(qry1(a[i]-1)-qry1(st[z[a[i]]]-1)+mod)*D[a[i]]%mod*S%mod*inv2%mod+mod)%mod;
ans=(ans+1ll*qry2(a[i]-1)*S)%mod;
mdf(a[i],ID[a[i]]);
}
printf("%d\n",ans);
return 0;
}
[agc23E]Inversions的更多相关文章
- [UCSD白板题] Number of Inversions
Problem Introduction An inversion of a sequence \(a_0,a_1,\cdots,a_{n-1}\) is a pair of indices \(0 ...
- Codeforces Round #301 (Div. 2) E . Infinite Inversions 树状数组求逆序数
E. Infinite Inversions ...
- Inversions After Shuffle
Inversions After Shuffle time limit per test 1 second memory limit per test 256 megabytes input stan ...
- 《算法导论》Problem 2-4 Inversions
在Merge Sort的基础上改改就好了. public class Inversions { public static int inversions(int [] A,int p, int r) ...
- Dynamic Inversions II 逆序数的性质 树状数组求逆序数
Dynamic Inversions II Time Limit: 6000/3000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Other ...
- Dynamic Inversions 50个树状数组
Dynamic Inversions Time Limit: 30000/15000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others ...
- [Swift]LeetCode775. 全局倒置与局部倒置 | Global and Local Inversions
We have some permutation Aof [0, 1, ..., N - 1], where N is the length of A. The number of (global) ...
- [LeetCode] Global and Local Inversions 全局与局部的倒置
We have some permutation A of [0, 1, ..., N - 1], where N is the length of A. The number of (global) ...
- 775. Global and Local Inversions
We have some permutation A of [0, 1, ..., N - 1], where N is the length of A. The number of (global) ...
随机推荐
- (转)如何让ActiveXObject( "Microsoft.XmlDom ")对象在非IE浏览器下显示数据?firefox(火狐)
如何让ActiveXObject( "Microsoft.XmlDom ")对象在非IE浏览器下显示数据?firefox(火狐) 2013-09-10 16:01 2152人阅读 ...
- Codeforces Round #523 (Div. 2) Solution
A. Coins Water. #include <bits/stdc++.h> using namespace std; int n, s; int main() { while (sc ...
- cookie的存取删
存: document.cookie = "name=Kevin;expires="+new Date().getDate()+7; //有效期7天 取: function Get ...
- MySql如何安装?
官方网址:https://www.mysql.com/downloads/ Community——>MySqlCommunity Server->GA->64位: GA:正式版: C ...
- Sybase数据库第三方软件安装
Sybase数据库第三方软件安装 1,安装软件:IQNC160011P_1-20011253.ZIP
- (转载)找圆算法((HoughCircles)总结与优化
Opencv内部提供了一个基于Hough变换理论的找圆算法,HoughCircle与一般的拟合圆算法比起来,各有优势:优势:HoughCircle对噪声点不怎么敏感,并且可以在同一个图中找出多个圆 ...
- Linux内核分析06
进程的描述和进程的创建 一,进程的描述 进程控制块PCB——task_struct (进程描述符),为了管理进程,内核必须对每个进程进行清晰的描述,进程描述符提供了内核所需了解的进程信息. struc ...
- arm-linux-ld命令
我们对每个c或者汇编文件进行单独编译,但是不去连接,生成很多.o 的文件,这些.o文件首先是分散的,我们首先要考虑的如何组合起来:其次,这些.o文件存在相互调用的关系:再者,我们最后生成的bin文件是 ...
- sqlite3 shell方向键、浏览历史命令不能用的问题
在sqlite3 shell下,按上下方向键可以浏览历史命令,按左右方向键则可以将光标移动到命令中任意字符位置,从而可以修改错误的语句或误敲的字符.所以方向键是sqlite3 shell下最常用的功能 ...
- 【转载】linux fork死循环炸弹及其预防
转自linux fork死循环炸弹及其预防 在Linux系统下执行这段代码 :(){ :|:& }:: 就会引起死机,一旦执行起来后,唯一的方法就是重启系统.实际上这段代码是一段无限递归代码, ...