[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) ...
随机推荐
- #C++初学记录(素数判断)
练习题目二 素数判断 A prime number is a natural number which has exactly two distinct natural number divisors ...
- poj1673 EXOCENTER OF A TRIANGLE
地址:http://poj.org/problem?id=1673 题目: EXOCENTER OF A TRIANGLE Time Limit: 1000MS Memory Limit: 100 ...
- 【4】Python对象
本章主题 Python对象 内建类型 标准类型操作符 值的比较 对象身份比较 布尔类型 标准类型内建函数 标准类型总览 各种类型 不支持的类型 Python对象 Python使用 ...
- python中 @property
考察 Student 类: class Student(object): def __init__(self, name, score): self.name = name self.score = ...
- 封装JS实现Ajax
这两天仔细理解了一下Ajax,然后整理封装了一下,如果有什么不对的地方,请指教,谢谢! AJAX AJAX = Asynchronous JavaScript and XML(异步的 JavaScri ...
- Python3基础 三元运算符 简单示例
Python : 3.7.0 OS : Ubuntu 18.04.1 LTS IDE : PyCharm 2018.2.4 Conda ...
- Linux清除Windows密码
下载安装ntfs-3g 下载驱动让linux挂载windows磁盘 https://tuxera.com/opensource/ntfs-3g_ntfsprogs-2017.3.23.tgz 安装 t ...
- java分布式系统开关功能设计(服务升降级)
问题一:在单个java系统中如何实现开关功能? 其实对于开关来说,对应Java中的类型,很好映射,就是一个boolean值,在需要做开关操作的地方,调用这个属性,判断状态,然后走相应的 ...
- SDN前瞻 软件定义网络的一些概念
SDN的核心:可编程性 SDN的思想:SOA面向服务 面向服务的体系结构(service-oriented architecture SOA) 使网络连接的大量计算机易于合作,以 服务 而不是人工交互 ...
- class []的用法
span[class='test'] =>匹配所有带有class类名test的span标签 span[class *='test'] =>匹配所有包含了test字符串的class类 ...