【洛谷 P5367】【模板】康托展开
题目大意:
给你一个排列,问你按照字典序它是第几个排列?
思路:
首先,最简单的暴力做法就是用深搜从头开始将所有排列按照字典序生成,看看这个排列在第几个位置。
时间复杂度
O
(
n
!
)
O(n!)
O(n!),然后
n
≤
1
0
6
n\leq 10^6
n≤106,你算到宇宙灭亡估计都算不出来。
所以这里我们引入康托展开,对于每一个排列
a
[
]
a [~]
a[ ],我们有:
f
[
i
]
=
∑
j
=
i
+
1
n
(
a
[
i
]
>
a
[
j
]
)
f[i]=\sum_{j=i+1}^{n}(a[i]>a[j])
f[i]=j=i+1∑n(a[i]>a[j])
a
n
s
=
1
+
∑
i
=
1
n
f
[
i
]
∗
(
n
−
i
)
!
ans=1+\sum_{i=1}^{n} f[i]*(n-i)!
ans=1+i=1∑nf[i]∗(n−i)!
具体证明我就不放了 ,因为我太菜了,这种东西背住模板才是王道。
接下来,你按照上面的式子模拟去做,交上去,一片 TLE
然后悲伤的你终于看到了
n
≤
1
0
6
n\leq10^6
n≤106
O
(
n
2
)
O(n^2)
O(n2)悍不畏死的冲上去,洛谷反手一个 TLE砸下来
那么我们发现这道题的瓶颈就在于如何快速求出
f
[
i
]
f[i]
f[i]
我刚开始的想法是用平衡树维护 感觉我是NT,然后我就憨憨滴去打了一个
f
h
q
T
r
e
a
p
fhq~~Treap
fhq Treap
100+行:
平衡树代码:
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<cstring>
#include<vector>
#include<cmath>
#include<ctime>
#define r register
#define rep(i,x,y) for(r ll i=x;i<=y;++i)
#define per(i,x,y) for(r ll i=x;i>=y;--i)
using namespace std;
typedef long long ll;
const ll V=1e6+10,p=998244353;
ll n,a[V],f[V],ans;
ll x,y,z,root,cnt;
struct fhqtreap
{
ll lc,rc,siz;
ll pos,val;
}treap[V];
ll newnode(ll x)
{
treap[++cnt].val=x;
treap[cnt].siz=1;
treap[cnt].pos=rand();
return cnt;
}
void update(ll x)
{
treap[x].siz=treap[treap[x].lc].siz+treap[treap[x].rc].siz+1;
}
void split(ll now,ll k,ll &x,ll &y)
{
if(!now)
{
x=y=0;
return ;
}
if(k>=treap[now].val)
{
x=now;
split(treap[now].rc,k,treap[now].rc,y);
}
else
{
y=now;
split(treap[now].lc,k,x,treap[now].lc);
}
update(now);
}
ll make(ll x,ll y)
{
if(!x||!y) return x+y;
if(treap[x].pos<treap[y].pos)
{
treap[x].rc=make(treap[x].rc,y);
update(x);
return x;
}
else
{
treap[y].lc=make(x,treap[y].lc);
update(y);
return y;
}
}
void add(ll k)
{
split(root,k,x,y);
root=make(make(x,newnode(k)),y);
}
void del(ll k)
{
split(root,k,x,z);
split(x,k-1,x,y);
y=make(treap[y].lc,treap[y].rc);
root=make(make(x,y),z);
}
ll K(ll now,ll k)
{
while(1+1==2) //搞笑专用
{
if(k<=treap[treap[now].lc].siz) now=treap[now].lc;
else if(k==treap[treap[now].lc].siz) return now;
else k-=(treap[treap[now].lc].siz+1),now=treap[now].rc;
}
}
inline ll in()
{
ll res=0,f=1;
char ch;
while((ch=getchar())<'0'||ch>'9')
if(ch=='-') f=-1;
res=res*10+ch-48;
while((ch=getchar())>='0'&&ch<='9')
res=res*10+ch-48;
return res*f;
}
int main()
{
srand(time(0));
n=in();
f[0]=1;
rep(i,1,n) f[i]=f[i-1]*i%p;
rep(i,1,n)
{
a[i]=in();
add(a[i]);
}
rep(i,1,n)
{
split(root,a[i]-1,x,y);
ans=(ans+(treap[x].siz)*f[n-i]%p)%p;
root=make(x,y);
del(a[i]);
}
cout<<ans+1;
return 0;
}
当然,我是不会告诉你这个代码只有 55 分,跑的和暴力差不多······

垃圾平衡树,毁我青春
虽然我也不知道是不是因为自己写错了还是因为平衡树的常数太大······
所以我们拿出了常数无敌者——树状数组。
在值域上维护每个数出现的次数,每次求前缀和就是有多少个比它小的数出现在了数组中,然后把这个数从数组中删去 ,就是在树状数组中 -1。
树状数组代码:
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<cstring>
#include<vector>
#include<cmath>
#define r register
#define rep(i,x,y) for(r ll i=x;i<=y;++i)
#define per(i,x,y) for(r ll i=x;i>=y;--i)
using namespace std;
typedef long long ll;
const ll V=1e6+10,p=998244353;
ll n,a[V],f[V],c[V],ans;
ll lowbit(ll x) { return x&(-x); }
void add(ll x,ll y)
{
for(;x<=n;x+=lowbit(x))
c[x]+=y;
return ;
}
ll ask(ll x)
{
ll res=0;
for(;x;x-=lowbit(x))
res+=c[x];
return res;
}
inline ll in()
{
ll res=0,f=1;
char ch;
while((ch=getchar())<'0'||ch>'9')
if(ch=='-') f=-1;
res=res*10+ch-48;
while((ch=getchar())>='0'&&ch<='9')
res=res*10+ch-48;
return res*f;
}
int main()
{
n=in();
f[0]=1;
rep(i,1,n)
{
a[i]=in();
f[i]=f[i-1]*i%p;
add(a[i],1);
}
rep(i,1,n)
{
add(a[i],-1);
ll val=ask(a[i]-1);
ans=(ans+val*f[n-i]%p)%p;
}
printf("%lld",ans+1);
return 0;
}
树状数组跑得飞快······

6034




被折叠的 条评论
为什么被折叠?



