康拓展开算法讲解及例题

算法功能

康拓展开用于解决:给定一个值为 1 1 1 ~ n n n的,数字互不重复的,且长度为 n n n的排序序列,康拓展开可以计算出 1 1 1 ~ n n n的全排列按照字典序排列,给定排列是第多少个。

算法公式

x = p 1 ⋅ ( n − 1 ) ! + p 2 ⋅ ( n − 2 ) ! + … + p n ⋅ 0 ! x=p_1 \cdot (n-1)!+p_{2} \cdot (n-2)!+\ldots+p_n \cdot 0! x=p1(n1)!+p2(n2)!++pn0!
x x x指的是当前给定的排列序列, p i p_i pi指的是第 i + 1 i+1 i+1 ~ n n n个值中,比排列序列中第 i i i个值小的元素的数量。

算法过程

比如说有一个序列 52413 52413 52413
首先先从 a 1 a_1 a1开始,值为 5 5 5,后面分别有 2 2 2 4 4 4 1 1 1 3 3 3,都比 5 5 5小,所以 p 1 p_1 p1的值为 4 4 4,那么当前 x x x就是 4 ∗ 4 ! = 96 4*4!=96 44!=96
接着到了 a 2 a_2 a2,值为 2 2 2,后面只有一个 1 1 1比他小,所以 p 2 p_2 p2的值就是 1 1 1,那么当前的 x x x就是 96 + 1 ∗ 3 ! = 102 96+1*3!=102 96+13!=102
然后就是 a 3 a_3 a3,值为 4 4 4,后面的 1 1 1 3 3 3都比他小,那么 p 3 p_3 p3就是 2 2 2,当前的 x x x就是 102 + 2 ∗ 2 ! = 106 102+2*2!=106 102+22!=106
最后是 a 4 a_4 a4,值是 1 1 1,后面没有比他小的,所以贡献为 0 0 0
那么52413是不是全排列的第 106 106 106个呢?不是。
比如我们有一个序列12345。
因为这是一个单调上升的序列,所以 p i p_i pi的值都是 0 0 0,所以最后的 x x x就是 0 0 0
所以,最后这个序列的排名就是康拓展开后 x + 1 x+1 x+1

算法分析

还是上面那个序列: 52413 52413 52413
先看 5 5 5,以 5 5 5开头的序列 52413 52413 52413,可以知道以 4 4 4 3 3 3 2 2 2 1 1 1开头的序列字典序都比这个序列小,后面的四个元素都可以任意组合,如:以 4 4 4开头时, 5 5 5 3 3 3 2 2 2 1 1 1可以在其之后任意排列,这 4 4 4个数共有 4 ! 4! 4!种排列方式,而由于 4 4 4 3 3 3 2 2 2 1 1 1 4 4 4个数开头时都比 52413 52413 52413字典序小,所以以 5 5 5开头时就会有 4 ∗ 4 ! 4*4! 44!种排列字典序更小。
接着是 2 2 2 2 2 2作第二位时,只有 1 1 1作第二位才会更小,所以这种情况就有 1 ∗ 3 ! 1*3! 13!种排列字典序更小。
……
剩下的数位同理。

代码

#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=1e6+5;
int read(){
	int x=0,f=1;
	char c=getchar();
	while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
	while(c>='0'&&c<='9')x=(x<<3)+(x<<1)+c-'0',c=getchar();
	return x*f;
}
void print(int x){
	if(x<0)putchar('-'),x=-x;
	if(x<10){putchar(x+'0');return;}
	print(x/10);
	putchar(x%10+'0');
}
int n;
int a[N];
int f[N]={1};
signed main(){
	n=read();
	for(int i=1;i<=n;i++)a[i]=read(),f[i]=f[i-1]*i;
	int ans=0;
	for(int i=1;i<=n;i++){
		int res=0;
		for(int j=i+1;j<=n;j++)
			if(a[j]<a[i])res++;
		ans+=res*f[n-i];
	}
	print(ans+1);
}

但是,这样做的时间复杂度是 O ( n 2 ) O(n^2) O(n2),我们还需要优化。
可以考虑用树状数组,将数组倒过来算。

#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=1e6+5;
int read(){
	int x=0,f=1;
	char c=getchar();
	while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
	while(c>='0'&&c<='9')x=(x<<3)+(x<<1)+c-'0',c=getchar();
	return x*f;
}
void print(int x){
	if(x<0)putchar('-'),x=-x;
	if(x<10){putchar(x+'0');return;}
	print(x/10);
	putchar(x%10+'0');
}
int n;
int a[N];
int bit[N];
void update(int x,int p){
	while(x<=n){
		bit[x]+=p;
		x+=x&-x;
	}
}
int query(int x){
	int res=0;
	while(x){
		res+=bit[x];
		x-=x&-x;
	}
	return res;
}
int f[N]={1};
signed main(){
	n=read();
	for(int i=1;i<=n;i++)a[n-i+1]=read();
	for(int i=1;i<n;i++)f[i]=f[i-1]*i;
	int ans=0;
	for(int i=1;i<=n;i++){
		int res=query(a[i]-1);
		ans+=f[i-1]*res;
		update(a[i],1);
	}
	print(ans+1);
}

时间复杂度 O ( n l o g n ) O(nlogn) O(nlogn)

例题

P5367 【模板】康托展开
注意要用树状数组优化时间复杂度,在树状数组里面记得取模。

#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=1e6+5;
const int mod=998244353;
int read(){
	int x=0,f=1;
	char c=getchar();
	while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
	while(c>='0'&&c<='9')x=(x<<3)+(x<<1)+c-'0',c=getchar();
	return x*f;
}
void print(int x){
	if(x<0)putchar('-'),x=-x;
	if(x<10){putchar(x+'0');return;}
	print(x/10);
	putchar(x%10+'0');
}
int n;
int a[N];
int bit[N];
void update(int x,int p){
	while(x<=n){
		bit[x]+=p;
		x+=x&-x;
	}
}
int query(int x){
	int res=0;
	while(x){
		res=(res+bit[x])%mod;
		x-=x&-x;
	}
	return res;
}
int f[N]={1};
signed main(){
	n=read();
	for(int i=1;i<=n;i++)a[n-i+1]=read();
	for(int i=1;i<n;i++)f[i]=f[i-1]*i%mod;
	int ans=0;
	for(int i=1;i<=n;i++){
		int res=query(a[i]-1);
		ans=(ans+f[i-1]*res%mod)%mod;
		update(a[i],1);
	}
	print(ans+1);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值