跳到正文

个人记录

如何 O(1) 排序

cancan123456约 19 分钟
原文
文章信息与版本

文章信息

发布
2022年10月4日
收录
2025年12月12日
更新
2026年8月14日
篇幅
约 19 分钟
获赞
3

版本

  1. v1当前

我们现做如下假设:

  1. 输入有 nn 个数,以一个二进制数构成,比如二进制数 11,1011,1110,111,1011,1110,1 压缩为二进制数 00011 01011 01110 0000100011\text{ }01011\text{ }01110\text{ }00001(请记住每个数字前面的 00,以后要考)。

  2. 输出以同样的方式压缩。

  3. 整数串中每个数的位数都必须相等,位数不够用零补足。这个位数称为定宽或 ww,本文的例子的 ww 为 44。

  4. 对于任意大小的整数,其赋值、四则运算、取余运算、比较运算、位运算都能够在 O(1)O(1) 的时间内完成。

  5. 没有重复。

那么我们开始。

考虑如下排序算法:

给定 nn 个数 a1,a2,a3,…,ana_1,a_2,a_3,\dots,a_n,对于每个数,枚举有几个数小于其,设有 xix_i 个数小于 aia_i,那么将 aia_i 存入 bxib_{x_i},那么 bib_i 就是 aia_i 排序的结果。

首先,假设我们要对 0100 0111 0001 00100100\text{ }0111\text{ }0001\text{ }0010 排序,我们考虑把原来的串扩展成 A,BA,B 两个串:

A=0100 0111 0001 0010 0100 0111 0001 0010 0100 0111 0001 0010 0100 0111 0001 0010A=0100\text{ }0111\text{ }0001\text{ }0010\text{ }0100\text{ }0111\text{ }0001\text{ }0010\text{ }0100\text{ }0111\text{ }0001\text{ }0010\text{ }0100\text{ }0111\text{ }0001\text{ }0010 B=0100 0100 0100 0100 0111 0111 0111 0111 0001 0001 0001 0001 0010 0010 0010 0010B=0100\text{ }0100\text{ }0100\text{ }0100\text{ }0111\text{ }0111\text{ }0111\text{ }0111\text{ }0001\text{ }0001\text{ }0001\text{ }0001\text{ }0010\text{ }0010\text{ }0010\text{ }0010

AA 比较好构造,我们只需要让原串乘 0000 0000 0000 0001 0000 0000 0000 0001 0000 0000 0000 0001 0000 0000 0000 0001 0000\text{ }0000\text{ }0000\text{ }0001\text{ }0000\text{ }0000\text{ }0000\text{ }0001\text{ }0000\text{ }0000\text{ }0000\text{ }0001\text{ }0000\text{ }0000\text{ }0000\text{ }0001\text{ },称为 maskmask,即可。

而我们有:

mask=1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 11111111 1111 1111 1111=264−1216−1mask=\frac{1111\text{ }1111\text{ }1111\text{ }1111\text{ }1111\text{ }1111\text{ }1111\text{ }1111\text{ }1111\text{ }1111\text{ }1111\text{ }1111\text{ }1111\text{ }1111\text{ }1111\text{ }1111}{1111\text{ }1111\text{ }1111\text{ }1111}=\frac{2^{64}-1}{2^{16}-1}

写成通用的形式,就是:

mask=2n2w−12nw−1mask=\frac{2^{n^2w}-1}{2^{nw}-1}

这样我们在后面就可以一起比较 O(n2)O(n^2) 个数对了。

我们将 BB 看做:

B=0000 0000 0000 0100 0000 0000 0000 0111 0000 0000 0000 0001 0000 0000 0000 0010×0001 0001 0001 0001B=0000\text{ }0000\text{ }0000\text{ }0100\text{ }0000\text{ }0000\text{ }0000\text{ }0111\text{ }0000\text{ }0000\text{ }0000\text{ }0001\text{ }0000\text{ }0000\text{ }0000\text{ }0010\times0001\text{ }0001\text{ }0001\text{ }0001

那么前面这个操作等价于加大 ww,这个操作不好做,但我们发现,如果我们把 A=0000 0000 0000 0000 0100 0111 0001 0010 0100 0111 0001 0010 0100 0111 0001 0010 0100 0111 0001 0010A=0000\text{ }0000\text{ }0000\text{ }0000\text{ }0100\text{ }0111\text{ }0001\text{ }0010\text{ }0100\text{ }0111\text{ }0001\text{ }0010\text{ }0100\text{ }0111\text{ }0001\text{ }0010\text{ }0100\text{ }0111\text{ }0001\text{ }0010 和 0000 0000 0000 0000 1111 0000 0000 0000 0000 1111 0000 0000 0000 0000 1111 0000 0000 0000 0000 11110000\text{ }0000\text{ }0000\text{ }0000\text{ }1111\text{ }0000\text{ }0000\text{ }0000\text{ }0000\text{ }1111\text{ }0000\text{ }0000\text{ }0000\text{ }0000\text{ }1111\text{ }0000\text{ }0000\text{ }0000\text{ }0000\text{ }1111 按位与一下,就可以得到:

0000 0000 0000 0000 0100 0000 0000 0000 0000 0111 0000 0000 0000 0000 0001 0000 0000 0000 0000 00100000\text{ }0000\text{ }0000\text{ }0000\text{ }0100\text{ }0000\text{ }0000\text{ }0000\text{ }0000\text{ }0111\text{ }0000\text{ }0000\text{ }0000\text{ }0000\text{ }0001\text{ }0000\text{ }0000\text{ }0000\text{ }0000\text{ }0010

这就很接近 BB 了,我们现在要缩小 ww。

如果 w′×n≤ww'\times n\le w,我们就可以将一个定宽 ww 的串缩减为定宽为 w′w' 的序列,具体来说,将

0000 0000 0000 0000 0100 0000 0000 0000 0000 0111 0000 0000 0000 0000 0001 0000 0000 0000 0000 00100000\text{ }0000\text{ }0000\text{ }0000\text{ }0100\text{ }0000\text{ }0000\text{ }0000\text{ }0000\text{ }0111\text{ }0000\text{ }0000\text{ }0000\text{ }0000\text{ }0001\text{ }0000\text{ }0000\text{ }0000\text{ }0000\text{ }0010

对 1111 1111 1111 11111111\text{ }1111\text{ }1111\text{ }1111 取模,就可以得到原来的 0100 0111 0001 00100100\text{ }0111\text{ }0001\text{ }0010(其实这个操作相当于把原来的数截成 1616 位的然后再相加)。

然后我们通过两次操作把定宽扩大到 (n+1)2w(n+1)^2w,然后再缩到 n⋅wn\cdot w,再乘 0001 0001 0001 00010001\text{ }0001\text{ }0001\text{ }0001,我们就得到了 BB。

然后我们拿到了 AA 和 BB。

我们考虑计算 a−ba-b,在计算机里是 a+(∼b)a+(\sim b),其中 ∼\sim 表示按位取反,如果最高位符号位是 11 那么说明是负数,也就是 a<ba<b,否则 a>ba>b。

所以我们取的 ww 应该是最大位数加 11。

那么我们就可以按照如下步骤操作:

0100 0111 0001 0010 0100 0111 0001 0010 0100 0111 0001 0010 0100 0111 0001 00100100\text{ }0111\text{ }0001\text{ }0010\text{ }0100\text{ }0111\text{ }0001\text{ }0010\text{ }0100\text{ }0111\text{ }0001\text{ }0010\text{ }0100\text{ }0111\text{ }0001\text{ }0010

A 串。

0100 0100 0100 0100 0111 0111 0111 0111 0001 0001 0001 0001 0010 0010 0010 00100100\text{ }0100\text{ }0100\text{ }0100\text{ }0111\text{ }0111\text{ }0111\text{ }0111\text{ }0001\text{ }0001\text{ }0001\text{ }0001\text{ }0010\text{ }0010\text{ }0010\text{ }0010

B 串。

1011 1011 1011 1011 1000 1000 1000 1000 1110 1110 1110 1110 1101 1101 1101 11011011\text{ }1011\text{ }1011\text{ }1011\text{ }1000\text{ }1000\text{ }1000\text{ }1000\text{ }1110\text{ }1110\text{ }1110\text{ }1110\text{ }1101\text{ }1101\text{ }1101\text{ }1101

对 BB 取反。

0111 0111 0111 0111 0111 0111 0111 0111 0111 0111 0111 0111 0111 0111 0111 0111 0111\text{ }0111\text{ }0111\text{ }0111\text{ }0111\text{ }0111\text{ }0111\text{ }0111\text{ }0111\text{ }0111\text{ }0111\text{ }0111\text{ }0111\text{ }0111\text{ }0111\text{ }0111\text{ }

一个掩码。

0011 0011 0011 0011 0000 0000 0000 0000 0110 0110 0110 0110 0101 0101 0101 01010011\text{ }0011\text{ }0011\text{ }0011\text{ }0000\text{ }0000\text{ }0000\text{ }0000\text{ }0110\text{ }0110\text{ }0110\text{ }0110\text{ }0101\text{ }0101\text{ }0101\text{ }0101

将 BB 中符号位置 00。

0111 1010 0100 0101 0100 0111 0001 0010 1010 1101 0111 1000 1001 1100 0110 01110111\text{ }1010\text{ }0100\text{ }0101\text{ }0100\text{ }0111\text{ }0001\text{ }0010\text{ }1010\text{ }1101\text{ }0111\text{ }1000\text{ }1001\text{ }1100\text{ }0110\text{ }0111

AA 和上面一个序列相加。

1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 10001000\text{ }1000\text{ }1000\text{ }1000\text{ }1000\text{ }1000\text{ }1000\text{ }1000\text{ }1000\text{ }1000\text{ }1000\text{ }1000\text{ }1000\text{ }1000\text{ }1000\text{ }1000

另外一个掩码。

0000 1000 0000 0000 0000 0000 0000 0000 1000 1000 0000 1000 1000 1000 0000 00000000\text{ }1000\text{ }0000\text{ }0000\text{ }0000\text{ }0000\text{ }0000\text{ }0000\text{ }1000\text{ }1000\text{ }0000\text{ }1000\text{ }1000\text{ }1000\text{ }0000\text{ }0000

两者按位与。

0000 0001 0000 0000 0000 0000 0000 0000 0001 0001 0000 0001 0001 0001 0000 00000000\text{ }0001\text{ }0000\text{ }0000\text{ }0000\text{ }0000\text{ }0000\text{ }0000\text{ }0001\text{ }0001\text{ }0000\text{ }0001\text{ }0001\text{ }0001\text{ }0000\text{ }0000

右移 w−1w-1 位。

最后这个结果中,11 表示 BB 对应位置比 AA 大,否则就是 AA 对应位置比 BB 大。

如果原来的数字串没有重复,那么这个里面应该有 n(n−1)2\dfrac{n(n-1)}2 个 11。

把结果对 1111 1111 1111 11111111\text{ }1111\text{ }1111\text{ }1111 取模,得到:

0010 0011 0000 00010010\text{ }0011\text{ }0000\text{ }0001

这里面第一个数 00100010 表示原串中有 22 个数比第一个数 01000100 小,我们将这个序列称为 indexindex。

我们先构造这个序列:

count=0000 0001 0010 0011count=0000\text{ }0001\text{ }0010\text{ }0011

也就是:

∑i=0n−1i2w(n−i−1)\sum_{i=0}^{n-1}i2^{w(n-i-1)}

我们直接 Mathematica 算一下:

∑i=0n−1i2w(n−i−1)=2nw+n−2wn−1(2w−1)2\sum_{i=0}^{n-1}i2^{w(n-i-1)}=\frac{2^{nw}+n-2^wn-1}{(2^w-1)^2}

然后我们把 indexindex 与 countcount 比较一下,套用上面的所有步骤:

0010 0010 0010 0010 0011 0011 0011 0011 0000 0000 0000 0000 0001 0001 0001 00010010\text{ }0010\text{ }0010\text{ }0010\text{ }0011\text{ }0011\text{ }0011\text{ }0011\text{ }0000\text{ }0000\text{ }0000\text{ }0000\text{ }0001\text{ }0001\text{ }0001\text{ }0001

(扩展的 indexindex 序列)

0000 0001 0010 0011 0000 0001 0010 0011 0000 0001 0010 0011 0000 0001 0010 00110000\text{ }0001\text{ }0010\text{ }0011\text{ }0000\text{ }0001\text{ }0010\text{ }0011\text{ }0000\text{ }0001\text{ }0010\text{ }0011\text{ }0000\text{ }0001\text{ }0010\text{ }0011

(扩展的 countcount 序列)

0001 0000 0000 0001 0001 0001 0000 0000 0000 0001 0000 0000 0000 0000 0000 00000001\text{ }0000\text{ }0000\text{ }0001\text{ }0001\text{ }0001\text{ }0000\text{ }0000\text{ }0000\text{ }0001\text{ }0000\text{ }0000\text{ }0000\text{ }0000\text{ }0000\text{ }0000

(对应位置上 indexindex 小)

0000 0000 0000 0000 0000 0000 0001 0000 0000 0000 0001 0001 0001 0000 0001 00010000\text{ }0000\text{ }0000\text{ }0000\text{ }0000\text{ }0000\text{ }0001\text{ }0000\text{ }0000\text{ }0000\text{ }0001\text{ }0001\text{ }0001\text{ }0000\text{ }0001\text{ }0001

(对应位置上 countcount 小)

我们用 or\text{or} 和 xor\text{xor} 提取出两个串都是 00 的位置:

0001 0001 0000 0001 0001 0001 0001 0000 0000 0001 0001 0001 0001 0000 0001 00010001\text{ }0001\text{ }0000\text{ }0001\text{ }0001\text{ }0001\text{ }0001\text{ }0000\text{ }0000\text{ }0001\text{ }0001\text{ }0001\text{ }0001\text{ }0000\text{ }0001\text{ }0001

两个序列按位或。

0001 0001 0001 0001 0001 0001 0001 0001 0001 0001 0001 0001 0001 0001 0001 00010001\text{ }0001\text{ }0001\text{ }0001\text{ }0001\text{ }0001\text{ }0001\text{ }0001\text{ }0001\text{ }0001\text{ }0001\text{ }0001\text{ }0001\text{ }0001\text{ }0001\text{ }0001

一个掩码。

0000 0000 0001 0000 0000 0000 0000 0001 0001 0000 0000 0000 0000 0001 0000 00000000\text{ }0000\text{ }0001\text{ }0000\text{ }0000\text{ }0000\text{ }0000\text{ }0001\text{ }0001\text{ }0000\text{ }0000\text{ }0000\text{ }0000\text{ }0001\text{ }0000\text{ }0000

两者异或。

0000 0000 1111 0000 0000 0000 0000 1111 1111 0000 0000 0000 0000 1111 0000 00000000\text{ }0000\text{ }1111\text{ }0000\text{ }0000\text{ }0000\text{ }0000\text{ }1111\text{ }1111\text{ }0000\text{ }0000\text{ }0000\text{ }0000\text{ }1111\text{ }0000\text{ }0000

乘 2w−12^w-1。

然后与 BB 按位与:

0000 0000 0100 0000 0000 0000 0000 0111 0001 0000 0000 0000 0000 0010 0000 00000000\text{ }0000\text{ }0100\text{ }0000\text{ }0000\text{ }0000\text{ }0000\text{ }0111\text{ }0001\text{ }0000\text{ }0000\text{ }0000\text{ }0000\text{ }0010\text{ }0000\text{ }0000

最后的数满足出现的数与原串相同,且每个数都在排序后的位置上,所以我们对 1111 1111 1111 11111111\text{ }1111\text{ }1111\text{ }1111 取模,就可以得到最终的结果:

0001 0010 0100 01110001\text{ }0010\text{ }0100\text{ }0111

好,前面说了这么多,我们放一下代码。

Python
def print_in_binary(num, n, w):
    res = bin(num)[2:]
    res = '0' * (n * w - len(res)) + res
    ans = ''
    for i in range(n):
        ans += res[i * w : i * w + w] + ' '
    print(ans)

def get_1(n):
    return (1 << n) - 1

def get_mask(n, w):
    return get_1(n * w) // get_1(w)

def get_count(n, w):
    return ((1 << n * w) + n - (n << w) - 1) // (((1 << w) - 1) ** 2)

def get_A(a, n, w):
    return a * get_mask(n, n * w)

def inc_w(a, n, w):
    return get_A(a, n, w) & (get_mask(n, (n + 1) * w) * get_1(w))

def get_B(a, n, w):
    B = inc_w(a, n, w)
    B = inc_w(B, n, w * (n + 1))
    B = B % get_1(w * (n + 1) * (n + 1) - w * n)
    B = B * get_mask(n, w)
    return B

def sort_in_binary(a, n, w):
    A = get_A(a, n, w)
    B = get_B(a, n, w)

    notB = get_1(n * n * w) ^ B
    notB = notB & (get_mask(n * n, w) * get_1(w - 1))

    index = ((notB + A) & (get_mask(n * n, w) << (w - 1))) >> (w - 1)
    index = index % get_1(n * w)

    count = get_count(n, w)

    indexB = get_B(index, n, w)
    countA = get_A(count, n, w)

    notindexB = get_1(n * n * w) ^ indexB
    notindexB = notindexB & (get_mask(n * n, w) * get_1(w - 1))

    index_small = ((notindexB + countA) & (get_mask(n * n, w) << (w - 1))) >> (w - 1)

    notcountA = get_1(n * n * w) ^ countA
    notcountA = notcountA & (get_mask(n * n, w) * get_1(w - 1))

    count_small = ((notcountA + indexB) & (get_mask(n * n, w) << (w - 1))) >> (w - 1)

    result = index_small | count_small
    result = result ^ get_mask(n * n, w)
    result = result * get_1(w)
    result = result & B

    answer = result % get_1(w * n)

    return answer

# test [00011, 01011, 01110, 00001]
#n = 4
#w = 5
#a = int("00011010110111000001", 2)

# test [0100, 0111, 0001, 0010]
n = 4
w = 4
a = int("0100011100010010", 2)

print_in_binary(sort_in_binary(a, n, w), n, w)

结束

结束

结束

结束

结束

结束

结束

结束

结束

结束

原文
存档评论14 条抓取于