[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) ...
随机推荐
- LCS 最长公共子序列
区别最长公共子串(连续) ''' LCS 最长公共子序列 ''' def LCS_len(x, y): m = len(x) n = len(y) dp = [[0] * (n + 1) for i ...
- BZOJ:3832: [Poi2014]Rally
题意: 给出$DAG$,询问删掉哪个点之后最长路径最短 思路: 我们令$f[x]$表示从最远的点到达它的距离,$g[x]$表示它能够到达最远的点的距离 那么对于$(x -> y)$一条边来说,它 ...
- uva1416 dijkstra
大白书P330 这题比较麻烦 给出一个n个节点m条边的无向图,每条边上有一个正权.令c等于每对节点的最短路长度之和.例n=3时, c = d(1,1)+d(1,2)+d(1,3)+d(2,1)+d(2 ...
- c++编译时打印宏定义
#pragma message("this is message") #pragma message只能打印字符串,如果想打印任何宏定义可使用: #define PRINT_MAC ...
- 337BRoutine Problem
/*给出你图片的长和宽的比例a:b 和摄像头的比例c:d,然后叫你求最后将图片放进摄像头 以后,剩余的面积比上摄像头的总面积,注意要化简为最简形式,而且摄像头要设置成至少一条边和图片相等 做法:先将两 ...
- JAVA实验一20145315
JAVA实验一实验报告 实验名称 Java开发环境的熟悉 实验内容 1.使用JDK编译.运行简单的Java程序: 2.使用IDEA 编辑.编译.运行.调试Java程序. 实验知识点 JVM.JRE.J ...
- hdu_2048 错排问题
错排问题本质上就是一个动态规划问题,其状态转移方程为: 记d[n]为n个人错排情况的总数. 那么策略可以描述为:分析第n个人错排的可能情况: 1)前n-1个人满足错排的情况,那么第n个人加入后还要错排 ...
- [转] sql server 跨数据库调用存储过程
A库存储过程: create PROCEDURE [dbo].[spAAAForTest] ( ) =null , ) =null ) AS BEGIN select N'A' AS a , N'B' ...
- java 生成验证Guid码
- 这是一份很详细的 Retrofit 2.0 使用教程(含实例讲解)
前言 在Andrroid开发中,网络请求十分常用 而在Android网络请求库中,Retrofit是当下最热的一个网络请求库 今天,我将献上一份非常详细Retrofit v2.0的使用教程,希望你们会 ...