题目 › usaco-1450

Interstellar Intervals

USACO 2024 December · 金组 · 2024-12 · L3 困难 · C++17 · 时限 2 秒 · 测试点等分

查看原题跳到结果

题面

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

It's the year $3000$, and Bessie became the first cow in space! During her journey between the stars, she found a number line with $N$ ($2 \leq N \leq 5 \cdot 10^5$) points, numbered from $1$ to $N$. All points are initially colored white. She can perform the following operation any number of times.

  • Choose a position $i$ within the number line and a positive integer $x$. Then, color all the points in the interval $[i, i + x - 1]$ red and all points in $[i + x, i + 2x - 1]$ blue. All chosen intervals must be disjoint (i.e. no points in $[i, i + 2x - 1]$ can be already colored red or blue). The entire interval must also fall within the number line (i.e. $1 \leq i \leq i + 2x - 1 \leq N$).

Farmer John gives Bessie a string $s$ of length $N$ consisting of characters $R$, $B$, and $X$. The string represents Farmer John's color preferences for each point: $s_i=R$ means the $i$'th point must be colored red, $s_i = B$ means the $i$'th point must be colored blue, and $s_i = X$ means there is no constraint on the color for the $i$'th point.

Help Bessie count the number of distinct ways for the number line to be colored while satisfying Farmer John's preferences. Two colorings are different if there is at least one corresponding point with a different color. Because the answer may be large, output it modulo $10^9+7$.

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

The first line contains an integer $N$.

The following line contains string $s$.

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

Output the number of distinct ways for the number line to be colored while satisfying Farmer John's preferences modulo $10^9+7$.

SAMPLE INPUT:

6
RXXXXB

SAMPLE OUTPUT:

5
Bessie can choose $i=1,x=1$ (i.e. color point $1$ red and point $2$ blue) and $i=3,x=2$ (i.e. color points $3,4$ red and points $5,6$ blue) to produce the coloring $RBRRBB$.

The other colorings are $RRBBRB$, $RBWWRB$, $RRRBBB$, and $RBRBRB$.

SAMPLE INPUT:

6
XXRBXX

SAMPLE OUTPUT:

6
The six colorings are $WWRBWW$, $WWRBRB$, $WRRBBW$, $RBRBWW$, $RBRBRB$, and $RRRBBB$.

SAMPLE INPUT:

12
XBXXXXRXRBXX

SAMPLE OUTPUT:

18

SCORING:

  • Input 4: $N\le 500$
  • Inputs 5-6: $N\le 10^4$
  • Inputs 7-13: All but at most $100$ characters in $s$ are $X$.
  • Inputs 14-23: No additional constraints

Problem credits: Chongtian Ma, Alex Liang

结果

Kimi K3.1 ProKimi

auto(max)

3/3

解出的采样 ↑

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

DeepSeek Flash V4.1-1017DeepSeek

auto(high)

3/3

解出的采样 ↑

  • 100采样 1 · 解出
  • 100采样 2 · 解出
  • 100采样 3 · 解出
  • 44.7k平均输出 token(含思考) ↓
  • 200.5输出速度 tokens/s ↑
  • 226 秒延迟 ↓
  • 1.31×CPU 时间 / 官方解 ↓
  • 不确定 1、同阶 2时间复杂度(相对官方解)

MiniMax M3.1 FlashMiniMax

auto(max)

3/3

解出的采样 ↑

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

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

代码

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

static const int MOD = 1000000007;

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

    int N;
    string s;
    cin >> N >> s;

    vector<int> nextB(N + 1, N), nextR(N + 1, N), prevR(N, -1);
    for (int i = N - 1; i >= 0; --i) {
        nextB[i] = (s[i] == 'B' ? i : nextB[i + 1]);
        nextR[i] = (s[i] == 'R' ? i : nextR[i + 1]);
    }
    int last = -1;
    for (int i = 0; i < N; ++i) {
        if (s[i] == 'R') last = i;
        prevR[i] = last;
    }

    vector<int> dp(N + 1, 0);
    array<vector<int>, 2> diff;
    diff[0].assign(N + 3, 0);
    diff[1].assign(N + 3, 0);

    long long active[2] = {0, 0};
    dp[0] = 1;

    for (int p = 0; p <= N; ++p) {
        int parity = p & 1;
        active[parity] += diff[parity][p];
        if (active[parity] >= MOD) active[parity] -= MOD;

        int val = dp[p] + (int)active[parity];
        if (val >= MOD) val -= MOD;
        dp[p] = val;

        if (p == N) break;
        if (val == 0) continue;

        if (s[p] == 'X') {
            dp[p + 1] += val;
            if (dp[p + 1] >= MOD) dp[p + 1] -= MOD;
        }

        int U = min((N - p) / 2, nextB[p] - p);
        if (U <= 0) continue;

        auto &D = diff[parity];

        // Add val to dp[p+2*x] for every x in [a,b].
        auto addInterval = [&](int a, int b) {
            if (a > b) return;
            int l = p + 2 * a;
            int r = p + 2 * b;

            int z = D[l] + val;
            if (z >= MOD) z -= MOD;
            D[l] = z;

            z = D[r + 2] - val;
            if (z < 0) z += MOD;
            D[r + 2] = z;
        };

        /*
         * For an R at distance d from p, the operation is invalid exactly
         * for half-lengths x in [d/2+1, d]. Only d <= 2*U-1 matters.
         * Merge these intervals; their union has O(log N) components.
         */
        int lastPos = p + 2 * U - 1;
        int rpos = nextR[p + 1];
        int cursor = 1;

        while (rpos <= lastPos) {
            int d = rpos - p;
            int L = d / 2 + 1;

            addInterval(cursor, L - 1);

            int c = d;
            while (c < U) {
                int far = prevR[p + 2 * c + 1];
                if (far > p + c) {
                    c = far - p;
                } else {
                    break;
                }
            }

            if (c >= U) {
                cursor = U + 1;
                break;
            }

            cursor = c + 1;
            int nextPos = p + 2 * c + 2;
            rpos = (nextPos > lastPos ? N : nextR[nextPos]);
        }

        addInterval(cursor, U);
    }

    cout << dp[N] << '\n';
    return 0;
}