【agc023E】Inversions(线段树,动态规划)
【agc023E】Inversions(线段树,动态规划)
题面
AT
给定\(a_i\),求所有满足\(p_i\le a_i\)的排列\(p\)的逆序对数之和。
题解
首先如何计算排列\(p\)的个数。
设\(cnt[i]\)表示\(a_k\ge i\)的个数,那么满足条件的\(p\)的总数就是\(\prod cnt[i]-(n-i)\)
大概就是从\(n\)开始填数,对于每个数字\(i\)而言,它一共有\(cnt[i]\)个位置可以填,但是后面的数字一共占用了\(n-i\)个位置,所以还剩下\(cnt[i]-(n-i)\)个位置可以填。
先讲讲\(O(n^2log)\)的做法。
枚举任意两个位置\(i,j,i\lt j\),考虑\(i,j\)之间形成逆序对的贡献。
分成三种情况讨论。
1.\(a_i=a_j\)
显然对于任意一种满足条件的方案,要么\(p_i>p_j\)要么\(p_i<p_j\),并且两两对称,所以就是总方案除\(2\)。
2.\(a_i<a_j\)
当\(p_j\)取\([a_i+1,a_j]\)这部分的值的时候,显然不会产生贡献,而当\(p_j\in[1,a_i]\)时,则和上面是一样的,但是注意一下,此时我们直接把\(a_j\)变成了\(a_i\),会对于总方案产生影响,\([a_i+1,a_j]\)这一段的\(cnt\)减小了,所以用一个线段树维护一下区间积,就可以方便的维护当前状态下的总方案,那么这一部分也很好算。
3.\(a_i>a_j\)
很麻烦,但是我们可以正难则反来算,用总方案减去\(p_i<p_j\)的方案数,转化成了第二种情况,也就是将\(a_i\)直接变成\(a_j\)。
这样子用线段树维护,时间复杂度\(O(n^2logn)\),用前后缀之类的东西维护可以做到\(O(n^2)\)。
代码如下:
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
using namespace std;
#define ll long long
#define MAX 200200
#define MOD 1000000007
#define inv2 500000004
#define lson (now<<1)
#define rson (now<<1|1)
inline int read()
{
int x=0;bool t=false;char ch=getchar();
while((ch<'0'||ch>'9')&&ch!='-')ch=getchar();
if(ch=='-')t=true,ch=getchar();
while(ch<='9'&&ch>='0')x=x*10+ch-48,ch=getchar();
return t?-x:x;
}
int fpow(int a,int b)
{
int s=1;
while(b){if(b&1)s=1ll*s*a%MOD;a=1ll*a*a%MOD;b>>=1;}
return s;
}
int n,a[MAX],tot,ans;
int cnt[MAX];
int t[2][MAX<<2];
void Build(int now,int l,int r)
{
if(l==r){t[1][now]=cnt[l]-1-(n-l);t[0][now]=cnt[l]-(n-l);return;}
int mid=(l+r)>>1;
Build(lson,l,mid);Build(rson,mid+1,r);
t[0][now]=1ll*t[0][lson]*t[0][rson]%MOD;
t[1][now]=1ll*t[1][lson]*t[1][rson]%MOD;
}
int Query(int now,int l,int r,int L,int R,int c)
{
if(L<=l&&r<=R)return t[c][now];
int mid=(l+r)>>1,ret=1;
if(L<=mid)ret=1ll*ret*Query(lson,l,mid,L,R,c)%MOD;
if(R>mid)ret=1ll*ret*Query(rson,mid+1,r,L,R,c)%MOD;
return ret;
}
int main()
{
n=read();tot=1;
for(int i=1;i<=n;++i)++cnt[a[i]=read()];
for(int i=n-1;i;--i)cnt[i]+=cnt[i+1];
for(int i=1;i<=n;++i)tot=1ll*tot*(cnt[i]-(n-i))%MOD;
if(!tot){puts("0");return 0;}
Build(1,1,n);
for(int i=1;i<=n;++i)
for(int j=i+1;j<=n;++j)
{
if(a[i]==a[j])ans=(ans+1ll*tot*inv2)%MOD;
else if(a[i]<a[j])
{
int d=1ll*tot*fpow(Query(1,1,n,a[i]+1,a[j],0),MOD-2)%MOD;
d=1ll*d*Query(1,1,n,a[i]+1,a[j],1)%MOD;
ans=(ans+1ll*d*inv2)%MOD;
}
else
{
int d=1ll*tot*fpow(Query(1,1,n,a[j]+1,a[i],0),MOD-2)%MOD;
d=1ll*d*Query(1,1,n,a[j]+1,a[i],1)%MOD;
ans=(ans+MOD-1ll*d*inv2%MOD+tot)%MOD;
}
}
printf("%d\n",ans);
return 0;
}
而这种方法的复杂度瓶颈在于枚举任意两个位置之间逆序对的贡献。
考虑这个能否优化。
首先我们记\(D_i=\frac{cnt[i]-1-(n-i)}{cnt[i]-(n-i)}\),如果要修改某个区间的总方案数,等价于用全局的总方案数乘上某一段区间。设\(S\)为全局总方案数,那么强制求改\(a[i],a[j]\)成一样的总方案数就是:\(S\times \prod_{i=min+1}^{max}D_i\)。这个东西很显然可以写成前缀的形式,也就是\(S=\frac{\prod_{i=1}^{max}D_i}{\prod_{i=1}^{min}D_i}\)。
这样子只需要维护前缀积然后求个和就好了。
注意下\(D_i\)可能为\(0\),所以求的时候要分段计算一下贡献就好了,具体实现见代码。
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
using namespace std;
#define ll long long
#define MAX 200200
#define MOD 1000000007
#define inv2 500000004
inline int read()
{
int x=0;bool t=false;char ch=getchar();
while((ch<'0'||ch>'9')&&ch!='-')ch=getchar();
if(ch=='-')t=true,ch=getchar();
while(ch<='9'&&ch>='0')x=x*10+ch-48,ch=getchar();
return t?-x:x;
}
int fpow(int a,int b)
{
int s=1;
while(b){if(b&1)s=1ll*s*a%MOD;a=1ll*a*a%MOD;b>>=1;}
return s;
}
int n,a[MAX],tot,ans;
int cnt[MAX];
int c1[MAX],c2[MAX];
int lb(int x){return x&(-x);}
void add(int x,int w){while(x<=n)c1[x]=(c1[x]+w)%MOD,++c2[x],x+=lb(x);}
int getsum1(int x){int ret=0;while(x)ret=(ret+c1[x])%MOD,x-=lb(x);return ret;}
int getsum2(int x){int ret=0;while(x)ret+=c2[x],x-=lb(x);return ret;}
int D[MAX],invD[MAX],zero[MAX],S[MAX];
int main()
{
n=read();tot=1;
for(int i=1;i<=n;++i)++cnt[a[i]=read()];
for(int i=n-1;i;--i)cnt[i]+=cnt[i+1];
for(int i=1;i<=n;++i)tot=1ll*tot*(cnt[i]-=(n-i))%MOD;
if(!tot){puts("0");return 0;}
S[0]=D[0]=1;
for(int i=1;i<=n;++i)
{
int x=1ll*(cnt[i]-1)*fpow(cnt[i],MOD-2)%MOD;
if(!x)S[zero[i]=zero[i-1]+1]=i,D[i]=D[i-1];
else zero[i]=zero[i-1],D[i]=1ll*D[i-1]*x%MOD;
invD[i]=fpow(D[i],MOD-2);
}
for(int i=1;i<=n;++i)
{
ans=(ans+1ll*(getsum1(a[i])-getsum1(S[zero[a[i]]]-1)+MOD)*D[a[i]]%MOD*tot%MOD*inv2%MOD)%MOD;
add(a[i],invD[a[i]]);
}
for(int i=1;i<=n;++i)c1[i]=c2[i]=0;
for(int i=n;i;--i)
{
ans-=1ll*(getsum1(a[i]-1)-getsum1(S[zero[a[i]]]-1)+MOD)*D[a[i]]%MOD*tot%MOD*inv2%MOD;
ans=(ans+1ll*getsum2(a[i]-1)*tot)%MOD;ans=(ans+MOD)%MOD;
add(a[i],invD[a[i]]);
}
printf("%d\n",ans);
return 0;
}
【agc023E】Inversions(线段树,动态规划)的更多相关文章
- POJ 2374 Fence Obstacle Course(线段树+动态规划)
Fence Obstacle Course Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 2524 Accepted: ...
- Codeforces 675E Trains and Statistic - 线段树 - 动态规划
题目传送门 快速的vjudge通道 快速的Codeforces通道 题目大意 有$n$个火车站,第$i$个火车站出售第$i + 1$到第$a_{i}$个火车站的车票,特殊地,第$n$个火车站不出售车票 ...
- Codeforces 750E New Year and Old Subsequence - 线段树 - 动态规划
A string t is called nice if a string "2017" occurs in t as a subsequence but a string &qu ...
- ACM学习历程—HDU5696 区间的价值(分治 && RMQ && 线段树 && 动态规划)
http://acm.hdu.edu.cn/showproblem.php?pid=5696 这是这次百度之星初赛2B的第一题,但是由于正好打省赛,于是便错过了.加上2A的时候差了一题,当时有思路,但 ...
- 线段树详解 (原理,实现与应用)(转载自:http://blog.csdn.net/zearot/article/details/48299459)
原文地址:http://blog.csdn.net/zearot/article/details/48299459(如有侵权,请联系博主,立即删除.) 线段树详解 By 岩之痕 目录: 一:综述 ...
- BZOJ_1672_[Usaco2005 Dec]Cleaning Shifts 清理牛棚_动态规划+线段树
BZOJ_1672_[Usaco2005 Dec]Cleaning Shifts 清理牛棚_动态规划+线段树 题意: 约翰的奶牛们从小娇生惯养,她们无法容忍牛棚里的任何脏东西.约翰发现,如果要使这群 ...
- 【arc073f】Many Moves(动态规划,线段树)
[arc073f]Many Moves(动态规划,线段树) 题面 atcoder 洛谷 题解 设\(f[i][j]\)表示第一个棋子在\(i\),第二个棋子在\(j\)的最小移动代价. 发现在一次移动 ...
- 【BZOJ5469】[FJOI2018]领导集团问题(动态规划,线段树合并)
[BZOJ5469][FJOI2018]领导集团问题(动态规划,线段树合并) 题面 BZOJ 洛谷 题解 题目就是让你在树上找一个最大的点集,使得两个点如果存在祖先关系,那么就要满足祖先的权值要小于等 ...
- Codeforces 834D The Bakery - 动态规划 - 线段树
Some time ago Slastyona the Sweetmaid decided to open her own bakery! She bought required ingredient ...
随机推荐
- RenderSprite小记
类型定义: /** @private */ public static const IMAGE:int = 0x01; /** @private */ public static const ALPH ...
- 【LeetCode算法题库】Day5:Roman to Integer & Longest Common Prefix & 3Sum
[Q13] Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Valu ...
- 互联网校招面试必备——Java多线程
本文首发于我的个人博客:尾尾部落 本文是我刷了几十篇一线互联网校招java后端开发岗位的面经后总结的多线程相关题目,虽然有点小长,但是面试前看一看,相信能帮你轻松啃下多线程这块大骨头. 什么是进程,什 ...
- javascript this(上)
javascript的this指向的是一个函数运行时动态绑定对象. this的4种常见的指向: 作为对象的方法调用 var obj={ name:"姚小白", getName:fu ...
- Protocol buffer的使用案例
Protocolbuffer(以下简称PB)是google 的一种数据交换的格式,它独立于语言,独立于平台.google 提供了多种语言的实现:java.c#.c++.go 和 python,每一种实 ...
- 简介make命令和makefile文件
一.为什么要用到 make 命令和 makefile 文件 在 Linux 下编写一个程序,每次编译都需要在命令行一行一行的敲命令.如果是一个很小的程序还好说,命令不怎的复杂,编译速度也挺快,但是对于 ...
- 工具 | Sublime
Sublime 前言 妈耶..\(Sublime\)的界面真的是太好看啦哭哭.. 我永远喜欢Sublime! 强推Sublime... 正文 自从暑假用上的Ubontu 一开始用的是\(gedit\) ...
- python基础知识-12-模块的了解
python其他知识目录 1.模块介绍: Python 模块(Module),是一个 Python 文件,以 .py 结尾,包含了 Python 对象定义和Python语句.模块让你能够有逻辑地组织你 ...
- TeamWork#3,Week5,Scrum Meeting 11.9
由于经验不足和储备知识不够,最近我们的项目遇到了一些技术问题,需要对项目进行重新计划.我们总结了经验教训,找出了问题所在,明确了要补充的知识,加紧学习,将会在一周之内解决相关问题. 成员 已完成 待完 ...
- Python写一个根据日期计算是星期几的模块
import datetimedef get_week_day(date): week_day = { 0: '星期一', 1: '星期二', 2: '星期三', 3: '星期四', 4: '星期五' ...