题解:P12667 [KOI 2023 Round 2] 傻瓜锁

· · 题解

有一个可以获得 55pts 的简单做法:考虑 DDP,容易写出转移矩阵,然后用线段树维护。时间复杂度 O(n|\Sigma|^3\log n),无法通过。这个 DP 并没有充分利用该问题的性质,并且转移矩阵是 (\min, +) 矩阵,此类矩阵很多都没有逆,通常性质也很差,所以基本可以看做没啥优化空间了。

考虑先固定当前最小可以使用的字母,假设是 a。那么按字典序从小到大加入可以使用的字母,假设当前已经加入了 a 和 b,最优的方案是 aaabbbbbbb,那么后面加入 c 后,一定形如 aaabbbbccc。也就是 a 和 b 之间的分割点一定不会改变。调整法易证。

这个性质也可以通过 01 转化来发现。具体而言,将字母转化为 1\sim 26 的整数,设置基准值 t,将 \ge t 的整数看做 1,其余看做 0。考虑枚举分界点 c,下标 \le c 的均为 0,>c 的均为 1,设这个 01 串为 A_c,原序列对应的 01 串为 B。那么总操作数可以看做 \sum_{t = 1}^{26} f(t)。其中 f(t) 表示基准值为 t 时,\min_{0\le c \le n}\{\operatorname{popcount}(A_c \oplus B)\} 的值。显然不同的 t 之间不会相互影响,且 t 越大,序列中的 0 越多,最优决策点 c 就越大,符合原问题的限制。

于是就做完了,开 26 颗线段树对每个基准值 t 维护一个 01 序列即可。时间复杂度 O(n|\Sigma|\log n)。

实际上这个 DP 是一个 2D / 1D 的 DP,在 DP 的优化技巧里这通常被称为决策单调性。和 01 转化得出的结论是相同的。

#include <bits/stdc++.h>
#define fi first
#define se second
#define eb(x) emplace_back(x)
#define pb(x) push_back(x)
#define lc (p << 1)
#define rc ((p << 1) | 1)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int uint;
typedef long double ldb;
typedef __int128 i128;
using pi = pair<int, int>;
const int N = 1e5 + 10;
const int inf = 0x3f3f3f3f;
int n, q, a[N];
char s[N];
bitset<N> arr[26];

struct Node {
    int l, r, res, all0, all1;
};

struct Segtree {
    Node tr[4 * N];
    void pushup(Node &p, Node ls, Node rs) {
        p.res = min({ls.all0 + rs.res, ls.res + rs.all1, ls.all0 + rs.all1});
        p.all0 = ls.all0 + rs.all0;
        p.all1 = ls.all1 + rs.all1;
    }
    void build(int id, int p, int l, int r) {
        tr[p] = {l, r, 0, (arr[id][l] != 0), (arr[id][l] != 1)};
        if(l == r) return;
        int mid = (l + r) >> 1;
        build(id, lc, l, mid);
        build(id, rc, mid + 1, r);
        pushup(tr[p], tr[lc], tr[rc]);
    }
    void update(int p, int pos, int val) {
        if(tr[p].l == pos && tr[p].r == pos) {
            tr[p].res = 0;
            tr[p].all0 = (val != 0);
            tr[p].all1 = (val != 1);
            return;
        }
        int mid = (tr[p].l + tr[p].r) >> 1;
        if(pos <= mid) update(lc, pos, val);
        else update(rc, pos, val);
        pushup(tr[p], tr[lc], tr[rc]);
    }
} tr[26];

void getans() {
    int ans = 0;
    for(int i = 0; i < 26; i++) {
        ans += tr[i].tr[1].res;
    }
    cout << ans << "\n";
}

int main()
{
    //freopen("sample.in", "r", stdin);
    //freopen("sample.out", "w", stdout);
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin >> s + 1;
    n = strlen(s + 1);
    for(int i = 1; i <= n; i++) {
        a[i] = s[i] - 'a';
        for(int j = 0; j <= a[i]; j++) {
            arr[j][i] = 1;
        }
    }
    for(int i = 0; i < 26; i++) {
        tr[i].build(i, 1, 1, n);
    }

    getans();
    cin >> q;
    while(q--) {
        int x; char c;
        cin >> x >> c;
        int val = c - 'a';
        a[x] = val;
        for(int j = 0; j < 26; j++) {
            arr[j][x] = (val >= j);
            tr[j].update(1, x, arr[j][x]);
        }
        getans();
    }
    return 0;
}