跳到正文

题解

哎可恶的斜率优化啊

dongzhenmao约 9 分钟
原文
文章信息与版本

文章信息

发布
2026年10月29日
收录
2026年11月2日
更新
2026年11月18日
篇幅
约 9 分钟
题目
P12550

版本

  1. v1当前

不存在两个相邻字串满足若不移动左右字串不可移动中间,而移动后可以移动中间。

据此,列出暴力转移:

f(i)=max⁡{f(j−1)+w(j,i)}f(i) = \max \{ f(j - 1) + w(j, i) \}

其中 s[j...i]s[j...i] 至多两种字符。

其中 jj 的左端点可以用双指针维护。

形式化的,先考虑 AB\texttt{AB},记 cc 从双色段开始到现在的数量前缀和,有:

w(j,i)=∑s[e≥j]=AicB(j...i)=cB(i)×(cA(i)−cA(j−1))−∑s[e≥j]=AicB(j−1)\begin{aligned} w(j, i) &= \sum_{s[e \ge j] = \texttt{A}} ^ i c_B(j...i) \\ &= c_B(i) \times (c_A(i) - c_A(j - 1)) - \sum_{s[e \ge j] = \texttt{A}} ^ i c_B(j - 1) \end{aligned}

这个东西容斥一下:

−∑s[e≥j]=AicB(j−1)=−∑s[e≤i]=AicB(j−1)+∑s[e<j]=AicB(j−1)- \sum_{s[e \ge j] = \texttt{A}} ^ i c_B(j - 1) = -\sum_{s[e \le i] = \texttt{A}} ^ i c_B(j - 1) + \sum_{s[e < j] = \texttt{A}} ^ i c_B(j - 1)

所以 jj 的贡献函数,传入 cB(i)c_B(i),就出来了:

gj(x)=−cntA(j−1)×x+f(j−1)+∑s[e<j]=AicB(j−1)g_j(x) = -cnt_A(j - 1) \times x + f(j - 1) + \sum_{s[e < j] = \texttt{A}} ^ i c_B(j - 1) T(i)=cA(i)×cB(i)−∑s[e≤i]=AicB(j−1)T(i) = c_A(i) \times c_B(i) -\sum_{s[e \le i] = \texttt{A}} ^ i c_B(j - 1) f(i)=max⁡{gj(cB(i))}+T(i)f(i) = \max \{g_j(c_B(i))\} + T(i)

如果当前并不是双色了,那直接清空李超线段树,然后重构,对于每个点最多被算两次,复杂度为 O(nlog⁡n)O(n\log n)。

C++
#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <cmath>
#include <random>
#include <cstring>
#include <queue>

namespace {
    typedef long long lnt;

    const int inf = 0x3f3f3f3f;
    const int mod = 998244353; 

    const int max_n = 1e6 + 10, max_k = 31;

}

struct line {
    lnt k, b;
    lnt operator ()(int x) { return k * x + b; }
    line() = default;
    line(lnt _k, lnt _b) : k(_k), b(_b) {}
} f[max_n];

int cmp(int a, int b, int x) { // 返回 f[a](x) < f[b](x)
    if (a == -1 || b == -1) return a == -1;
    lnt va = f[a](x), vb = f[b](x);
    return a == b ? (a > b) : (va < vb);
}

int maxln(int a, int b, int x) { return cmp(a, b, x) ? b : a; }

struct segt { // 0-index
    inline static int lf(int x) { return x << 1; }
    inline static int rt(int x) { return lf(x) | 1; }

    int id[max_n << 2];
    int _m_R;

    void _build(int cur, int ll, int rr) {
        id[cur] = -1;
        if (!(ll == rr - 1)) {
            int mid = (ll + rr) >> 1;
            _build(lf(cur), ll, mid), _build(rt(cur), mid, rr);
        }
    }

    void _clear(int cur, int ll, int rr) {
        id[cur] = -1;
        if (!(ll == rr - 1)) {
            int mid = (ll + rr) >> 1;
            if (id[lf(cur)] != -1) _clear(lf(cur), ll, mid);
            if (id[rt(cur)] != -1) _clear(rt(cur), mid, rr);
        }
    }

    void build(int n) {
        _m_R = n, _build(1, 0, _m_R);
    }

    void clear() {
        _clear(1, 0, _m_R);
    }

    void _update(int cur, int ll, int rr, int _id) {
        int mid = (ll + rr) >> 1;
        if (id[cur] == -1 || cmp(id[cur], _id, mid)) std::swap(id[cur], _id);
        if (ll == rr - 1 || _id == -1) return;
        if (cmp(id[cur], _id, ll))     _update(lf(cur), ll, mid, _id);
        if (cmp(id[cur], _id, rr - 1)) _update(rt(cur), mid, rr, _id); // else 实际上只有一种
    }

    int _query(int cur, int ll, int rr, int b) {
        if (ll == rr - 1) {
            return id[cur];
        } else {
            int mid = (ll + rr) >> 1;
            if (b < mid) return maxln(id[cur], _query(lf(cur), ll, mid, b), b);
            else         return maxln(id[cur], _query(rt(cur), mid, rr, b), b);
        }
    }

    int query(int b) {
        return _query(1, 0, _m_R, b);
    }

    void update(int _id) {
        _update(1, 0, _m_R, _id);
    }

} t;

int n;
lnt dp[max_n];
std::string s;

void _solve(int l, int r, int LF, int RT) {
    std::vector<int> cnt(2, 0);
    t.update(0);
    lnt s1 = 0;
    for (int i = l; i <= r; ++i) {
        f[i] = line(-cnt[0], dp[i - 1] + s1);
        t.update(i);
        ++cnt[s[i] == RT];
        if (s[i] == LF) s1 += cnt[1];
        lnt t_i = lnt(cnt[0]) * cnt[1] - s1;
        dp[i] = std::max({dp[i], dp[i - 1], f[t.query(cnt[1])](cnt[1]) + t_i});
    }
    t.clear();
}

void solve() {
    std::cin >> n >> s, s.insert(s.begin(), '\0'), s.insert(s.end(), 'D');
    std::fill(dp, dp + n + 1, 0);

    std::vector<int> cnt(4, 0); 
    for (int i = 1, l = 1, now_c = 0; i <= n + 1; ++i) {
        now_c += (++cnt[s[i] - 'A'] == 1);
        if (now_c == 3) {
            --cnt[s[i] - 'A'];
            int LF, RT;
            if (cnt[0] && cnt[1]) LF = 'A', RT = 'B';
            if (cnt[1] && cnt[2]) LF = 'B', RT = 'C';
            if (cnt[2] && cnt[0]) LF = 'C', RT = 'A';
            _solve(l, i - 1, LF, RT);
            ++cnt[s[i] - 'A'];
            while (now_c != 2) now_c -= (--cnt[s[l++] - 'A'] == 0);
        }
    }
    std::printf("%lld\n", dp[n]);
}

int main() {
    t.build(max_n);

    std::cin.tie(nullptr)->sync_with_stdio(false);

    int T = 1; std::cin >> T;
    for (int i = 0; i < T; ++i) {
        solve();
    }

    return 0;
}
原文