将一个排列映射到一个数的方法就叫做康托展开。它的具体做法是这样的,对于一个给定的排列{ai}(i=1,2,3...n),对于每个ai求有多少个aj,使得j>i且ai>aj,简单来说就是求ai后面有多少数比ai小,假设我们求出来了这样的排列的一个对应数组{bi},其中bi就是ai后面有多少个数比它小。那么这个排列对应的康托展开即为∑bi*(n-i)!.

ai={1 3 5 4 2}

bi={0 1 2 1 0} 对应的排列数  0*4!+1*3!+2*2!+1*1!+0*0!.

bi可以通过树状数组在O(nlogn)里得到。

至于逆康托展开,就是已知bi,反求出ai,即知道了{0 1 2 1 0}.

做法也是类似的,我们看第一个数是0,表示后面有0个比它小的数,那么它必然是1.然后看第二个数是1,表示这个数在除去1 后面有1个比它小的数,那么这个数就是3,然后2,表示这个数后面在除去1,3,之后有2个比它小的数,所以它是5,以此往下。

一个O(nlogn)的想法是这样的,建立一棵平衡树,把1~n都插进去,每次查找第(bi+1)大的数,即为第i个数,然后把这个数删掉,以此往复。

但手写一个支持找第k大的平衡树比较慢,另外一个想法是在bit里把每个数都置1,然后二分出第一个使得前缀和为bi+1的数,这样做的复杂度就变成了O(nlog^2n).

#pragma warning(disable:4996)
#include <iostream>
#include <cstring>
#include <string>
#include <vector>
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std; #define maxn 210000 int bit[maxn];
int n; void upd(int i)
{
while (i <= n){
bit[i] += 1;
i += i&(-i);
}
} void dec(int i)
{
while (i <= n){
bit[i] -= 1;
i += i&(-i);
}
} int cal(int i)
{
int ret = 0;
while (i > 0){
ret += bit[i];
i -= i&(-i);
}
return ret;
} int solve(int x)
{
int l = 1, r = n+1;
while (l < r)
{
int mid = (l + r) >> 1;
int tx = cal(mid);
if (tx < x) l = mid + 1;
else r = mid;
}
return l;
} int a[maxn];
int b[maxn];
int c[maxn];
int va[maxn];
int vb[maxn];
int vc[maxn]; int main()
{
while (cin >> n)
{
for (int i = 0; i < n; ++i){
scanf("%d", a + i); a[i]++;
}
for (int i = 0; i < n; ++i){
scanf("%d", b + i); b[i]++;
}
memset(bit, 0, sizeof(bit));
for (int i = n - 1; i >= 0; --i){
va[i] = cal(a[i]);
upd(a[i]);
}
memset(bit, 0, sizeof(bit));
for (int i = n - 1; i >= 0; --i){
vb[i] = cal(b[i]);
upd(b[i]);
}
reverse(va, va + n);
reverse(vb, vb + n);
memset(vc, 0, sizeof(vc));
int carry = 0;
for (int i = 1; i < n; ++i){
vc[i] = (carry + va[i] + vb[i]) % (i + 1);
carry = (carry + va[i] + vb[i]) / (i + 1);
}
reverse(vc, vc + n);
memset(bit, 0, sizeof(bit));
for (int i = 1; i <= n; ++i){
upd(i);
}
for (int i = 0; i < n; ++i){
c[i] = solve(vc[i]+1);
dec(c[i]);
}
for (int i = 0; i < n; ++i){
if (i) printf(" ");
printf("%d", c[i]-1);
}
puts("");
}
return 0;
}

CF501D Misha and Permutations Summation(康托展开)的更多相关文章

  1. Codeforces Round #285 (Div. 1) B - Misha and Permutations Summation 康拓展开+平衡树

    思路:很裸的康拓展开.. 我的平衡树居然跑的比树状数组+二分还慢.. #include<bits/stdc++.h> #define LL long long #define fi fir ...

  2. Misha and Permutations Summation

    A - Misha and Permutations Summation 首先这个 mod n! 因为数量级上的差别最多只会对康拓展开的第一项起作用所以这个题并不需要把 ord (p) 和 ord ( ...

  3. [Codeforces 501D] - Misha and Permutations Summation

    题意是给你两个长度为$n$的排列,他们分别是$n$的第$a$个和第$b$个全排列.输出$n$的第$\left(a+b \right)\textrm{mod} \, n!$个全排列. 一种很容易的想法是 ...

  4. 【codeforces 501D】Misha and Permutations Summation

    [题目链接]:http://codeforces.com/problemset/problem/501/D [题意] 给你两个排列; 求出它们的字典序num1和num2; 然后让你求出第(num1+n ...

  5. Codeforces Round #285 (Div.1 B & Div.2 D) Misha and Permutations Summation --二分+树状数组

    题意:给出两个排列,求出每个排列在全排列的排行,相加,模上n!(全排列个数)得出一个数k,求出排行为k的排列. 解法:首先要得出定位方法,即知道某个排列是第几个排列.比如 (0, 1, 2), (0, ...

  6. 用康托展开实现全排列(STL、itertools)

    康拓展开: $X=a_n*(n-1)!+a_{n-1}*(n-2)!+\ldots +a_2*1!+a_1*0!$ X=an*(n-1)!+an-1*(n-2)!+...+ai*(i-1)!+...+ ...

  7. leetcode 60. Permutation Sequence(康托展开)

    描述: The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of t ...

  8. 【BZOJ】3301: [USACO2011 Feb] Cow Line(康托展开)

    http://www.lydsy.com/JudgeOnline/problem.php?id=3301 其实这一题很早就a过了,但是那时候看题解写完也是似懂非懂的.... 听zyf神犇说是康托展开, ...

  9. UVA11525 Permutation[康托展开 树状数组求第k小值]

    UVA - 11525 Permutation 题意:输出1~n的所有排列,字典序大小第∑k1Si∗(K−i)!个 学了好多知识 1.康托展开 X=a[n]*(n-1)!+a[n-1]*(n-2)!+ ...

随机推荐

  1. WIFI共享大师无法开启发射功能

    1.打开服务(ctrl+R)输入services.msc 2.将关于wifi的服务打开 这里有windows移动热点服务和WLAN开头的服务

  2. Windows Server 2008 正式版下载汇总

    windows 2008是微软推出的新一代服务器专用系统版本, 具有良好的用户体验以及应用程序,windows 2008大幅提升了web服务以及应用程序的性能, 让企业在提供和维护资源服务的时候更加得 ...

  3. c语言——字符串

    char str[] = "http://c.biancheng.net"; char str[] = "http://c.biancheng.net"; ch ...

  4. [BZOJ2947]促销(Splay)

    Description Great Bytelandish的超级市场网络请你编写一个程序模拟促销商品的成本费用(simulating costs of the promotionbeing prepa ...

  5. [Hdu3555] Bomb(数位DP)

    Description 题意就是找0到N有多少个数中含有49. \(1\leq N \leq2^{63}-1\) Solution 数位DP,与hdu3652类似 \(F[i][state]\)表示位 ...

  6. Birthday Paradox

    Birthday Paradox Sometimes some mathematical results are hard to believe. One of the common problems ...

  7. 使用Visual Studio建立报表--C#

    原文:使用Visual Studio建立报表--C# 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/qq_23893313/article/deta ...

  8. Android 本地css引用

    /** 全局web样式 * 以前看不懂,现在仔细,耐心的看看,全懂了,认真的看一遍都懂了 * * * */ // 链接样式文件,代码块高亮的处理 public final static String ...

  9. hadoop伪分布式集群的搭建

    集群配置: jdk1.8.0_161 hadoop-2.6.1 linux系统环境:Centos6.5 创建普通用户  dummy 设置静态IP地址 Hadoop伪分布式集群搭建: 为普通用户添加su ...

  10. MySQL基础7-分页查询

    1.分页查询(MySQL特有的,oracle中没有) 栗子1: 每页最多3条记录:pageSize=3:第一页:SELECT * FROM product LIMIT 0,3第二页:SELECT * ...