题目 › usaco-1517

It's Mooin' Time III

USACO 2025 US Open · 铜组 · 2025-03 · L1 基础 · Python 3.11 · 时限 4 秒 · 测试点等分

查看原题跳到结果

题面

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

Elsie is trying to describe her favorite USACO contest to Bessie, but Bessie is having trouble understanding why Elsie likes it so much. Elsie says "And It's mooin' time! Who wants a mooin'? Please, I just want to do USACO".

Bessie still doesn't understand, so she transcribes Elsie's description in a string of length $N$ ($3 \leq N \leq 10^5$) containing lowercase alphabetic characters $s_1s_2 \ldots s_N$. Elsie considers a string $t$ containing three characters a moo if $t_2 = t_3$ and $t_2 \neq t_1$.

A triplet $(i, j, k)$ is valid if $i < j < k$ and string $s_i s_j s_k$ forms a moo. For the triplet, FJ performs the following to calculate its value:

  • FJ bends string $s$ 90-degrees at index $j$
  • The value of the triplet is twice the area of $\Delta ijk$.

In other words, the value of the triplet is $(j-i)(k-j)$.

Bessie asks you $Q$ ($1 \leq Q \leq 3 \cdot 10^4$) queries. In each query, she gives you two integers $l$ and $r$ ($1 \leq l \leq r \leq N$, $r-l+1 \ge 3$) and ask you for the maximum value among valid triplets $(i, j, k)$ such that $l \leq i$ and $k \leq r$. If no valid triplet exists, output $-1$.

Note that the large size of integers involved in this problem may require the use of 64-bit integer data types (e.g., a "long long" in C/C++).

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

The first line contains two integers $N$ and $Q$.

The following line contains $s_1 s_2, \ldots s_N$.

The following $Q$ lines contain two integers $l$ and $r$, denoting each query.

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

Output the answer for each query on a new line.

SAMPLE INPUT:

12 5
abcabbacabac
1 12
2 7
4 8
2 5
3 10

SAMPLE OUTPUT:

28
6
1
-1
12
For the first query, ($i,j,k$) must satisfy $1 \le i < j < k \le 12$. It can be shown that the maximum area of $\Delta ijk$ for some valid ($i,j,k$) is achieved when $i=1$, $j=8$, and $k=12$. Note that $s_1 s_8 s_{12}$ is the string "acc" which is a moo according to the definitions above. $\Delta ijk$ will have legs of lengths $7$ and $4$ so two times the area of it will be $28$.

For the third query, ($i,j,k$) must satisfy $4 \le i < j < k \le 8$. It can be shown that the maximum area of $\Delta ijk$ for some valid ($i,j,k$) is achieved when $i=4$, $j=5$, and $k=6$.

For the fourth query, there exists no ($i,j,k$) satisfying $2 \le i < j < k \le 5$ in which $s_i s_j s_k$ is a moo so the output to that query is $-1$.

SCORING:

  • Inputs 2-3: $N,Q\le 50$
  • Inputs 4-6: $Q=1$ and the singular query satisfies $l=1$ and $r=N$
  • Inputs 7-11: No additional constraints

Problem credits: Chongtian Ma

结果

Kimi K3.1 ProKimi

auto(max)

1/1

解出的采样 ↑

  • 100采样 1 · 解出
  • 6.8k平均输出 token(含思考) ↓
  • 29.7输出速度 tokens/s ↑
  • 231 秒延迟 ↓
  • 0.35×CPU 时间 / 官方解 ↓
  • 不确定时间复杂度(相对官方解)
  • 7Pylint 问题 ↓
  • 8.79Pylint 得分 ↑
  • 17圈复杂度 ↓
  • 61.1可维护性指数 ↑

DeepSeek Flash V4.1-1017DeepSeek

auto(high)

1/1

解出的采样 ↑

  • 100采样 1 · 解出
  • 13.9k平均输出 token(含思考) ↓
  • 200.5输出速度 tokens/s ↑
  • 70 秒延迟 ↓
  • 0.34×CPU 时间 / 官方解 ↓
  • 不可测时间复杂度(相对官方解)
  • 10Pylint 问题 ↓
  • 8.44Pylint 得分 ↑
  • 17圈复杂度 ↓
  • 37.9可维护性指数 ↑

MiniMax M3.1 FlashMiniMax

auto(max)

1/1

解出的采样 ↑

  • 100采样 1 · 解出
  • 18.5k平均输出 token(含思考) ↓
  • 418.9输出速度 tokens/s ↑
  • 45 秒延迟 ↓
  • 0.22×CPU 时间 / 官方解 ↓
  • 同阶时间复杂度(相对官方解)
  • 9Pylint 问题 ↓
  • 8.73Pylint 得分 ↑
  • 24圈复杂度 ↓
  • 53.5可维护性指数 ↑

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

代码

Kimi K3.1 Pro · 采样 1 · 92 行
import sys
from bisect import bisect_left, bisect_right


def main():
    data = sys.stdin.buffer.read().split()
    if not data:
        return

    n = int(data[0])
    q = int(data[1])
    s = data[2]

    occurrences = [[] for _ in range(26)]
    for i, ch in enumerate(s):
        occurrences[ch - 97].append(i)

    # next_different[i] is the first position after i whose character
    # differs from s[i], skipping the entire run starting at i.
    next_different = [n] * n
    for i in range(n - 2, -1, -1):
        if s[i] == s[i + 1]:
            next_different[i] = next_different[i + 1]
        else:
            next_different[i] = i + 1

    answers = []
    idx = 3

    for _ in range(q):
        l = int(data[idx]) - 1
        r = int(data[idx + 1]) - 1
        idx += 2

        first_char = s[l] - 97
        after_initial_run = next_different[l]
        best = -1

        # Let y be the repeated character at positions j and k.
        for y in range(26):
            positions = occurrences[y]
            if len(positions) < 2:
                continue

            # For fixed y and j, the best i is the earliest position >= l
            # containing a character different from y.
            if y == first_char:
                i = after_initial_run
            else:
                i = l

            # For fixed i and j, using the last possible k is optimal.
            k_index = bisect_right(positions, r) - 1
            if k_index < 0:
                continue
            k = positions[k_index]

            if i >= k:
                continue

            # j must be an occurrence of y with i < j < k.
            first_j_index = bisect_right(positions, i)
            if first_j_index >= k_index:
                continue

            # (j-i)(k-j) is maximized by the available j closest to
            # the midpoint (i+k)/2. Only its neighboring occurrences
            # need to be checked.
            target = (i + k) // 2
            p = bisect_left(
                positions, target, first_j_index, k_index
            )

            if p < k_index:
                j = positions[p]
                value = (j - i) * (k - j)
                if value > best:
                    best = value

            if p > first_j_index:
                j = positions[p - 1]
                value = (j - i) * (k - j)
                if value > best:
                    best = value

        answers.append(str(best))

    sys.stdout.write("\n".join(answers))


if __name__ == "__main__":
    main()