题解:P11361 [NOIP2024] 编辑字符串

· · 题解

P11361 [NOIP2024] 编辑字符串

\text{Description}

给定两个二进制串和两个掩码,可以在掩码的限制下任意地交换两个相邻比特,求在任意次操作后,两个二进制串对应位置相同的字符数最多为多少。

\text{Solution}

约定与记号

问题分析

首先,我们容易观察到,如果 t_{1, [l, r]} = 1,那么在 [l, r] 这一段连续的区间里,s_{1, [l, r]} 是可以随意排列的,但 1 与 0 的个数不变。

对于同一个位置 i,s_{1, i} 和 s_{2, i} 有如下的关系:

为什么先满足一个自由度的时候更优?其实还是挺显然的。

因为两个自由度的位置与 s_1, s_2 的初始排列无关,其对答案产生的贡献只和这个联通块的 0/1 的数量有关。

而一个自由度的位置要求特定的 0/1,如果先满足了两个自由度的位置则无法满足。

实现

这道题的代码挺不好实现的,因此也有人把这道题当大模拟。

具体的,预处理出极大的 [l, r],满足 t_{1/2, [l, r]} = 1,然后把它当作一个连通块,统计其中 s_{1/2, [l, r]} 的 0 与 1 的个数。

然后扫整个字符串两遍,第一次尝试满足自由度为 1 的位置,第二次尝试满足自由度为 2 和 0 的位置(0 无法满足,统计 [s_{1, i} = s_{2, i}] 即可)。

可以使用 \texttt{std::bitset} 优化,虽然时间复杂度不会更优,但有利于利用 CPU 缓存。

upd: 民间数据跑的比正常写法慢了近 10 倍。

更多的细节见代码。

\text{Code}

#include <bits/extc++.h>

#define inline __always_inline
template <typename T> inline void read(T &x)
{
    char ch;
    for (ch = getchar(); !isdigit(ch); ch = getchar());
    for (x = 0; isdigit(ch); ch = getchar()) x = x * 10 + (ch - '0');
}
const int MaxN = 1e5 + 5;

std::bitset<MaxN> s, t, u, v;
int n, cs[MaxN][2], ct[MaxN][2], hs[MaxN], ht[MaxN];
inline void blk(const std::bitset<MaxN> &s, const std::bitset<MaxN> &u, decltype(cs) &cnt, int h) { for (int i = h; u[i]; i++) cnt[h][s[i]]++; }
void test_case()
{
    read(n);
    memset(cs, 0, sizeof(cs)), memset(ct, 0, sizeof(ct));   // 多测不清空,OI 一场空
    memset(hs, 0, sizeof(hs)), memset(ht, 0, sizeof(ht));
    s.reset(), t.reset(), u.reset(), v.reset();
    std::cin >> s >> t >> u >> v;
    for (int i = 0, h = -1; i < n; i++)
    {
        if (h == -1 && u[i]) h = i, blk(s, u, cs, h);       // 统计连通块
        else if (h != -1 && !u[i]) h = -1;
        hs[i] = h;
    }
    for (int i = 0, h = -1; i < n; i++)
    {
        if (h == -1 && v[i]) h = i, blk(t, v, ct, h);
        else if (h != -1 && !v[i]) h = -1;
        ht[i] = h;
    }
    auto p = u ^ v; int ans = 0;
    for (int i = p._Find_first(); i < n; i = p._Find_next(i))   // 自由度为 1
        if (u[i])                                               // 我们尝试满足 t[i]
            cs[hs[i]][t[i]] ? ans++, cs[hs[i]][t[i]]-- : cs[hs[i]][!t[i]]--;
        else                                                    // 我们尝试满足 s[i]
            ct[ht[i]][s[i]] ? ans++, ct[ht[i]][s[i]]-- : ct[ht[i]][!s[i]]--;
    p.flip();
    for (int i = p._Find_first(); i < n; i = p._Find_next(i))
        if (!u[i]) ans += s[i] == t[i];                         // 自由度为 0,统计答案即可。
        else                                                    // 自由度为 2,上贪心
        {
            int zero = std::min(cs[hs[i]][0], ct[ht[i]][0]);
            int one =  std::min(cs[hs[i]][1], ct[ht[i]][1]);
            ans += zero + one;
            cs[hs[i]][0] -= zero, ct[ht[i]][0] -= zero;
            cs[hs[i]][1] -= one,  ct[ht[i]][1] -= one;
            if (!u[i + 1])                                      // 扣除无用的 0/1
                ct[ht[i]][0] -= cs[hs[i]][1], ct[ht[i]][1] -= cs[hs[i]][0];
            if (!v[i + 1])
                cs[hs[i]][0] -= ct[ht[i]][1], cs[hs[i]][1] -= ct[ht[i]][0];
        }
    printf("%d\n", ans);
}
int main()
{
    int t; read(t);
    while (t--) test_case();
    return 0;
}

\text{Miscs}

这是我在 NOIP 考场上 AC 的唯一一道题目,也是送我退役的题目。

在考场上,我这道题目做了 \text{1.5 h},并因此心态大乱而读错了 T2 的题面,最终含泪离场。

谨以这篇题解纪念我死去的 OI,也祝诸君不要步我的后尘。

AFOed on 2024/11/30.