题目 › usaco-1453

It's Mooin' Time

USACO 2024 December · 铂金组 · 2024-12 · L4 顶尖 · C++17 · 时限 3 秒 · 测试点等分

查看原题跳到结果

题面

来源:USACO 官网。模型收到的是同一份题面的文字。

**Note: The time limit for this problem is 3s, 1.5x the default.**

Bessie has a string of length $N$ ($1\le N\le 3\cdot 10^5$) consisting solely of the characters M and O. For each position $i$ of the string, there is a cost $c_i$ to change the character at that position to the other character ($1\le c_i\le 10^8$).

Bessie thinks the string will look better if it contains more moos of length $L$ ($1\le L\le \min(N, 3)$). A moo of length $L$ is an M followed by $L-1$ Os.

For each positive integer $k$ from $1$ to $\lfloor N/L\rfloor$ inclusive, compute the minimum cost to change the string to contain at least $k$ substrings equal to a moo of length $L$.

INPUT FORMAT (input arrives from the terminal / stdin):

The first line contains $L$ and $N$.

The next line contains Bessie's length-$N$ string, consisting solely of Ms and Os.

The next line contains space-separated integers $c_1\dots c_N$.

OUTPUT FORMAT (print output to the terminal / stdout):

Output $\lfloor N/L\rfloor$ lines, the answer for each $k$ in increasing order.

SAMPLE INPUT:

1 4
MOOO
10 20 30 40

SAMPLE OUTPUT:

0
20
50
90

SAMPLE INPUT:

3 4
OOOO
50 40 30 20

SAMPLE OUTPUT:

40

SAMPLE INPUT:

2 20
OOOMOMOOOMOOOMMMOMOO
44743602 39649528 94028117 50811780 97338107 30426846 94909807 22669835 78498782 18004278 16633124 24711234 90542888 88490759 12851185 74589289 54413775 21184626 97688928 10497142

SAMPLE OUTPUT:

0
0
0
0
0
12851185
35521020
60232254
99881782
952304708

SAMPLE INPUT:

3 20
OOOMOMOOOMOOOMMMOMOO
44743602 39649528 94028117 50811780 97338107 30426846 94909807 22669835 78498782 18004278 16633124 24711234 90542888 88490759 12851185 74589289 54413775 21184626 97688928 10497142

SAMPLE OUTPUT:

0
0
0
44743602
119332891
207066974

SCORING:

  • Input 5: $L=3, N\le 5000$
  • Input 6: $L=1$
  • Inputs 7-10: $L=2$
  • Inputs 11-18: $L=3$

Problem credits: Benjamin Qi

结果

Kimi K3.1 ProKimi

max

3/3

解出的采样 ↑

  • 100采样 1 · 解出
  • 100采样 2 · 解出
  • 100采样 3 · 解出
  • 22.3k平均输出 token(含思考) ↓
  • 22.2输出速度 tokens/s ↑
  • 914 秒延迟 ↓
  • 1.03×CPU 时间 / 官方解 ↓
  • 同阶 3时间复杂度(相对官方解)

Kimi K3.1 Pro(high)Kimi high

high

3/3

解出的采样 ↑

  • 100采样 1 · 解出
  • 100采样 2 · 解出
  • 100采样 3 · 解出
  • 22.3k平均输出 token(含思考) ↓
  • 22.2输出速度 tokens/s ↑
  • 914 秒延迟 ↓
  • 1.03×CPU 时间 / 官方解 ↓
  • 同阶 3时间复杂度(相对官方解)

DeepSeek Flash V4.1-1017DeepSeek

auto(high)

2/3

解出的采样 ↑

  • 100采样 1 · 解出
  • 100采样 2 · 解出
  • 0.0采样 3 · 无代码
  • 95.3k平均输出 token(含思考) ↓
  • 209.5输出速度 tokens/s ↑
  • 384 秒延迟 ↓
  • 1.45×CPU 时间 / 官方解 ↓
  • 同阶 2时间复杂度(相对官方解)

MiniMax M3.1 FlashMiniMax

auto(max)

0/3

解出的采样 ↑

  • 16.7采样 1 · 答案错误
  • 0.0采样 2 · 编译错误
  • 0.0采样 3 · 编译错误
  • 61.8k平均输出 token(含思考) ↓
  • 98.9输出速度 tokens/s ↑
  • 636 秒延迟 ↓
  • —CPU 时间 / 官方解 ↓
  • —时间复杂度(相对官方解)

↑ 越大越好,↓ 越小越好。得分 0–100,按官方时限判定,所有测试点都满分才算解出。CPU 时间 / 官方解为解出的答案在官方解最慢的 3 个测试点上的 CPU 时间之比(几何平均),只比较官方解至少需要 0.05 秒的题;复杂度按增长幂次相对官方解分类。判题与计分见 原理。

代码

Kimi K3.1 Pro · 采样 1 · 208 行
#include <bits/stdc++.h>
using namespace std;

using ll = long long;
const ll INF = (1LL << 62);
const int BLOCK = 16;

struct Func {
    // Values are defined for k = lo, ..., lo + inc.size().
    // value(lo) = base, and inc contains successive differences.
    int lo = -1;
    ll base = 0;
    vector<ll> inc;

    bool empty() const { return lo == -1; }
};

using Matrix = array<Func, 9>;

static Func singletonFunc(int k, ll value) {
    Func f;
    f.lo = k;
    f.base = value;
    return f;
}

static Func convolve(const Func& f, const Func& g) {
    if (f.empty() || g.empty()) return Func();

    Func r;
    r.lo = f.lo + g.lo;
    r.base = f.base + g.base;
    r.inc.resize(f.inc.size() + g.inc.size());
    merge(f.inc.begin(), f.inc.end(),
          g.inc.begin(), g.inc.end(), r.inc.begin());
    return r;
}

static Func pointwiseMin(Func* fs, int n) {
    if (n == 0) return Func();
    if (n == 1) return std::move(fs[0]);

    int lo = INT_MAX, hi = INT_MIN;
    for (int i = 0; i < n; i++) {
        lo = min(lo, fs[i].lo);
        hi = max(hi, fs[i].lo + (int)fs[i].inc.size());
    }

    vector<ll> values(hi - lo + 1, INF);
    for (int i = 0; i < n; i++) {
        const Func& f = fs[i];
        ll value = f.base;
        int at = f.lo - lo;
        for (int j = 0; j <= (int)f.inc.size(); j++) {
            values[at + j] = min(values[at + j], value);
            if (j < (int)f.inc.size()) value += f.inc[j];
        }
    }

    Func r;
    r.lo = lo;
    r.base = values[0];
    r.inc.resize(values.size() - 1);
    for (int i = 1; i < (int)values.size(); i++) {
        r.inc[i - 1] = values[i] - values[i - 1];
    }
    return r;
}

static Matrix combine(const Matrix& a, const Matrix& b, int states) {
    Matrix c;
    for (int leftState = 0; leftState < states; leftState++) {
        for (int rightState = 0; rightState < states; rightState++) {
            Func tmp[3];
            int cnt = 0;
            for (int midState = 0; midState < states; midState++) {
                const Func& f = a[leftState * states + midState];
                const Func& g = b[midState * states + rightState];
                if (!f.empty() && !g.empty()) {
                    tmp[cnt++] = convolve(f, g);
                }
            }
            if (cnt) {
                c[leftState * states + rightState] = pointwiseMin(tmp, cnt);
            }
        }
    }
    return c;
}

static Matrix makeBlock(const vector<ll>& weight, int l, int r, int states) {
    Matrix result;
    int len = r - l;
    int maxK = (len + states - 1) / states;

    ll dp[3][BLOCK + 1], ndp[3][BLOCK + 1];

    for (int initial = 0; initial < states; initial++) {
        for (int s = 0; s < states; s++) {
            for (int k = 0; k <= maxK; k++) dp[s][k] = INF;
        }
        dp[initial][0] = 0;

        for (int pos = l; pos < r; pos++) {
            for (int s = 0; s < states; s++) {
                for (int k = 0; k <= maxK; k++) ndp[s][k] = INF;
            }

            for (int s = 0; s < states; s++) {
                for (int k = 0; k <= maxK; k++) {
                    ll cur = dp[s][k];
                    if (cur == INF) continue;

                    int skipState = (s == 0 ? 0 : s - 1);
                    ndp[skipState][k] = min(ndp[skipState][k], cur);

                    if (s == 0 && k < maxK) {
                        ndp[states - 1][k + 1] =
                            min(ndp[states - 1][k + 1], cur + weight[pos]);
                    }
                }
            }

            for (int s = 0; s < states; s++) {
                for (int k = 0; k <= maxK; k++) dp[s][k] = ndp[s][k];
            }
        }

        for (int ending = 0; ending < states; ending++) {
            int lo = 0;
            while (lo <= maxK && dp[ending][lo] == INF) lo++;
            if (lo > maxK) continue;

            int hi = maxK;
            while (dp[ending][hi] == INF) hi--;

            Func f;
            f.lo = lo;
            f.base = dp[ending][lo];
            for (int k = lo + 1; k <= hi; k++) {
                f.inc.push_back(dp[ending][k] - dp[ending][k - 1]);
            }
            result[initial * states + ending] = std::move(f);
        }
    }
    return result;
}

static Matrix buildDP(const vector<ll>& weight, int l, int r, int states) {
    if (r - l <= BLOCK) return makeBlock(weight, l, r, states);
    int mid = (l + r) / 2;
    Matrix left = buildDP(weight, l, mid, states);
    Matrix right = buildDP(weight, mid, r, states);
    return combine(left, right, states);
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int L, N;
    cin >> L >> N;

    string s;
    cin >> s;

    vector<ll> cost(N);
    for (ll& x : cost) cin >> x;

    int m = N - L + 1;
    vector<ll> weight(m);
    for (int i = 0; i < m; i++) {
        ll w = 0;
        if (s[i] != 'M') w += cost[i];
        for (int j = 1; j < L; j++) {
            if (s[i + j] != 'O') w += cost[i + j];
        }
        weight[i] = w;
    }

    int K = N / L;

    if (L == 1) {
        sort(weight.begin(), weight.end());
        ll answer = 0;
        for (int k = 0; k < K; k++) {
            answer += weight[k];
            cout << answer << '\n';
        }
        return 0;
    }

    Matrix root = buildDP(weight, 0, m, L);
    Func endings[3];
    int cnt = 0;
    for (int state = 0; state < L; state++) {
        if (!root[state].empty()) endings[cnt++] = std::move(root[state]);
    }
    Func answerFunc = pointwiseMin(endings, cnt);

    ll answer = answerFunc.base;
    for (int k = 1; k <= K; k++) {
        answer += answerFunc.inc[k - answerFunc.lo - 1];
        cout << answer << '\n';
    }

    return 0;
}