并不玄学的卡常题:WC2017 挑战
CodingJellyfish · · 题解
大家好,我是水母哥。在听到很多人提到这道毒瘤题后,我做了这道题。下面我讲述一下我做这道题的做法与心路历程。
在此之前,声明一点:这篇题解并没有使用循环展开。
35 分
第一题,整数排序,时间复杂度要求线性,可以基数排序,不会的可以自学(类似于桶排)。
基数排序在桶大小为 256 时最快,因为一级缓存可以装下它(不要问我是怎么知道的)。
关于基数排序实现方式的解释见代码。
#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 个选手中有几个胜出,就是
__builtin_popcount(第一排的石头 & 第二排的剪刀) +
__builtin_popcount(第一排的剪刀 & 第二排的布) +
__builtin_popcount(第一排的布 & 第二排的石头)
// __builtin_popcount 指二进制位中 1 的个数,速度很快
这样运行效率便能成倍提升(运行时间大概能降低到原来的 1/32)。
#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 分
括号序列,容易想到
79 分
滚动数组 + 指针遍历 + memmove 就可以大大加快速度。
优化 1:滚动数组
优化 2:memmove
若