跳到正文

题解

并不玄学的卡常题:WC2017 挑战

CodingJellyfish约 14 分钟
原文
文章信息与版本

文章信息

发布
2021年9月6日
收录
2025年12月12日
更新
2026年11月14日
篇幅
约 14 分钟
获赞
89
题目
P4604

版本

  1. v1当前

大家好,我是水母哥。在听到很多人提到这道毒瘤题后,我做了这道题。下面我讲述一下我做这道题的做法与心路历程。

在此之前,声明一点:这篇题解并没有使用循环展开。

35 分

第一题,整数排序,时间复杂度要求线性,可以基数排序,不会的可以自学(类似于桶排)。

基数排序在桶大小为 256 时最快,因为一级缓存可以装下它(不要问我是怎么知道的)。

关于基数排序实现方式的解释见代码。

C
#define MSORT 200000005

unsigned bucket[256];

#define SORTBYTE(TYPE, FR, TO, LEN, BIT) {\ //宏定义函数,减小常数。
    memset(bucket, 0, sizeof(bucket));\
    for (TYPE *it = (FR) + LEN; it != (FR); it--)\ // 使用指针倒序遍历,减少下标运算。
        ++bucket[(it[-1] >> BIT) & 255];\ // 将数放到对应的桶里。
    for (unsigned *it = bucket; it != bucket + 255; it++)\
        it[1] += it[0];\ // 将桶转化成桶的前缀和
    for (TYPE *it = (FR) + LEN; it != (FR); it--)\
        (TO)[--bucket[(it[-1] >> BIT) & 255]] = it[-1];\ // 将数按桶记录的值放到对应的位置。
}

void Sorting_main(void)
{
    int n;
    u32 seed;
    static u32 a[MSORT], b[MSORT]; // 滚动数组。
    scanf("%d%u", &n, &seed);

    int i;
    for (i = 0; i < n; i++)
    {
        seed = next_integer(seed);
        a[i] = seed;
    }

    SORTBYTE(u32, a, b, n, 0); //(0 - 7 二进制位排序)
    SORTBYTE(u32, b, a, n, 8); //(8 - 15 二进制位排序)
    SORTBYTE(u32, a, b, n, 16); //(16 - 23 二进制位排序)
    SORTBYTE(u32, b, a, n, 24); //(24 - 31 二进制位排序)

    output_arr(a, n);
}

于是我就这样得了 35 分。

65 分

石头剪刀布区间运算。暴力运算必然超时。

在经过一段时间的思考后,我发现可以将石头剪刀布分别放入三个 int 中,三个 int 的二进制位分别表示 是否为石头 是否为剪刀 是否为布。这样一次可以运算 32 个选手。

求这 32 个选手中有几个胜出,就是

C
__builtin_popcount(第一排的石头 & 第二排的剪刀) +
__builtin_popcount(第一排的剪刀 & 第二排的布) + 
__builtin_popcount(第一排的布 & 第二排的石头)
// __builtin_popcount 指二进制位中 1 的个数,速度很快

这样运行效率便能成倍提升(运行时间大概能降低到原来的 1/32)。

C
#define MGAME 300005

typedef struct
{
    u32 paper;
    u32 scissor;
    u32 stone;
}
Stat;

void Game_main(void)
{
    int n, q;
    static char s1[MGAME];
    static char s2[MGAME];

    scanf("%d%d", &n, &q);
    scanf("%s%s", s1, s2);

    static Stat st1[32][MGAME / 32 + 1], st2[32][MGAME / 32 + 1];

    for (int i = 0; i < n; i++)
    {
        if (s1[i] == '2')
            st1[0][i >> 5].paper ^= 1 << (i & 31);
        else if (s1[i] == '1')
            st1[0][i >> 5].scissor ^= 1 << (i & 31);
        else if (s1[i] == '0')
            st1[0][i >> 5].stone ^= 1 << (i & 31);

        if (s2[i] == '2')
            st2[0][i >> 5].paper ^= 1 << (i & 31);
        else if (s2[i] == '1')
            st2[0][i >> 5].scissor ^= 1 << (i & 31);
        else if (s2[i] == '0')
            st2[0][i >> 5].stone ^= 1 << (i & 31);
    } // 压入 int 中。
    for (int i = 1; i < 32; i++)
    {
        for (int j = 0; j < (n + 31) >> 5; j++)
        {
            st1[i][j].stone = (st1[i - 1][j].stone >> 1) + ((st1[i - 1][j + 1].stone & 1) << 31);
            st1[i][j].paper = (st1[i - 1][j].paper >> 1) + ((st1[i - 1][j + 1].paper & 1) << 31);
            st1[i][j].scissor = (st1[i - 1][j].scissor >> 1) + ((st1[i - 1][j + 1].scissor & 1) << 31);
            st2[i][j].stone = (st2[i - 1][j].stone >> 1) + ((st2[i - 1][j + 1].stone & 1) << 31);
            st2[i][j].paper = (st2[i - 1][j].paper >> 1) + ((st2[i - 1][j + 1].paper & 1) << 31);
            st2[i][j].scissor = (st2[i - 1][j].scissor >> 1) + ((st2[i - 1][j + 1].scissor & 1) << 31);
        } // 预处理从每一个位置开始的 int,比如 1~32,2~33,3~34……方便运算。
    }
    static u32 anss[MGAME];
    for (int i = 0; i < q; i++)
    {
        int x, y, len;
        scanf("%d%d%d", &x, &y, &len);
        u32 dx = x & 31, dy = y & 31;
        u32 block_len = len >> 5;
        Stat *it1 = st1[dx] + (x >> 5), *it2 = st2[dy] + (y >> 5);
        u32 ans = 0;
        Stat *it1_end = st1[dx] + (x >> 5) + block_len;
        while (it1 < it1_end) // 使用指针顺序遍历,减少下标运算。
        {
            ans += __builtin_popcount((it1->paper & it2->stone)
                                    | (it1->scissor & it2->paper)
                                    | (it1->stone & it2->scissor));
            it1++;
            it2++;
        } // 求赢的个数。
        ans += __builtin_popcount(((it1->paper & it2->stone)
                                 | (it1->scissor & it2->paper)
                                 | (it1->stone & it2->scissor)) & ((1 << (len & 31)) - 1)); // 求剩下的赢的个数。
                                
        anss[i] = ans;
    }

    output_arr(anss, q);
}

以为过不了,结果居然恰恰相反(210ms)。

74 分

括号序列,容易想到 O(n2)\mathrm{O}(n^2) 的 dp,不过必然超时。

fi,j={fi−1,j−1+fi−1,j+1,当前为问号fi−1,j−1,当前为左括号fi−1,j+1,当前为右括号\begin{aligned} f_{i,j} &= \left\{\begin{matrix} &f_{i - 1,j - 1} + f_{i - 1,j + 1} \text{,当前为问号} \\ &f_{i-1,j-1} \text{,当前为左括号}\\ &f_{i - 1,j + 1} \text{,当前为右括号} \end{matrix}\right. \end{aligned}

79 分

滚动数组 + 指针遍历 + memmove 就可以大大加快速度。

优化 1:滚动数组

fj={fj−1+fj+1,当前为问号fj−1,当前为左括号fj+1,当前为右括号\begin{aligned} f_{j} &= \left\{\begin{matrix} &f_{j - 1} + f_{j + 1} \text{,当前为问号} \\ &f_{j - 1} \text{,当前为左括号}\\ &f_{j + 1} \text{,当前为右括号} \end{matrix}\right. \end{aligned}

优化 2:memmove

若 fj=fj−1f_j = f_{j-1} ,那么使用 stdlib.h 中的 memmove() (常数很小) 将整个 dp 数组向右移动一格,fj=fj+1f_j = f_{j+1} 就向左一格。fj=fj−1+fj+1f_j = f_{j-1} + f_{j+1} 就在向左移动一格的基础上 fj=fj+fj−2f_j = f_j + f_{j - 2}。

优化 3:指针遍历

fj=fj+fj+2f_j = f_j + f_{j + 2} 时用指针遍历,减少下标运算。

于是我得了 79 分。

93 分

接下来,我在观察 dp 数组时,发现在遍历到第单数个字符时,第双数个 dp 值必然是 0,第双数个字符时第单数个 dp 值必然是 0。可以再次优化。

优化 4:奇偶优化

(下标从 0 开始)

下标为单数:

fj={fj+fj−1,当前为问号fj−1,当前为左括号fj,当前为右括号\begin{aligned} f_{j} &= \left\{\begin{matrix} &f_{j} + f_{j - 1} \text{,当前为问号} \\ &f_{j - 1} \text{,当前为左括号}\\ &f_{j} \text{,当前为右括号} \end{matrix}\right. \end{aligned}

下标为双数:

fj={fj+fj+1,当前为问号fj,当前为左括号fj+1,当前为右括号\begin{aligned} f_{j} &= \left\{\begin{matrix} &f_{j} + f_{j + 1} \text{,当前为问号} \\ &f_{j} \text{,当前为左括号}\\ &f_{j + 1} \text{,当前为右括号} \end{matrix}\right. \end{aligned}

可以拿到 93 分。

100 分

优化 5:缩短 dp 数组长度

然后,我发现在遍历一定字符数时,下标较大的 dp 值没有参与答案的运算,可以忽略它们,缩短 dp 数组长度。详细方式见代码。这样就可以 AC 啦!

C
#define MIN(a, b) ((a) < (b) ? (a) : (b))

void Parentheses_main(void)
{
    int n;
    scanf("%d", &n);

    static char s[MPAIR];
    scanf("%s", s);

    static u32 dp[MPAIR / 2] = {1}; // 优化 1
    int cur_len = 0;
    for (int i = 0; i < n; i++)
    {
        while (cur_len * 2> n - i) // 优化 5
        {
            dp[cur_len--] = 0;
        }
        if (s[i] == '?')
        {
            u32 tmp_dp0 = dp[0];
            if ((i & 1) == 0) // 优化 4
                memmove(dp, dp + 1, (cur_len + 1) * sizeof(u32)), dp[cur_len] = 0; // 优化 2
            if ((i & 1) == 1) // 优化 4
                cur_len++;
            register u32 *it = dp + cur_len;
            for (; it > dp; it--)
            {
                it[0] += it[-1]; // 优化 3
            }
            if ((i & 1) == 0) // 优化 4
                dp[0] += tmp_dp0;
        }
        else if (s[i] == '(')
        {
            if ((i & 1) == 1) // 优化 4
            {
                memmove(dp + 1, dp, (cur_len + 1) * sizeof(u32)); // 优化 2
                dp[0] = 0;
                cur_len++;
            }
        }
        else if (s[i] == ')')
        {
            if (cur_len == 0 && (i & 1) == 0) // 一定不合法时跳出。
            {
                printf("0");
                return;
            }
            if ((i & 1) == 0) // 优化 4
            {
                memmove(dp, dp + 1, (cur_len + 1) * sizeof(u32)); // 优化 2
                dp[cur_len] = 0;
                cur_len--;
            }
        }
    }

    printf("%u\n", dp[0]);
}

综上,这道题并非使用 i++ 改成 ++i,或是快读,甚至是循环展开这一系列玄学、不可靠、降低代码可读性的优化,而是不停地减少不必要的运算。所以这道题考验的,更多是对算法的优化能力,而不是所谓的“硬件知识”。

原文
存档评论29 条抓取于