题目 › usaco-1544

Sliding Window Summation

USACO 2026 First · 银组 · 2026-01 · L2 中等 · Python 3.11 · 时限 4 秒 · 测试点等分

查看原题跳到结果

题面

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

Bessie has a hidden binary string $b_1b_2\dots b_N$ ($1\le N\le 2\cdot 10^5$). The only information about $b$ you are given is a binary string $r_1r_2\dots r_{N-K+1}$ ($1 \le K \le N$), where $r_i$ is the remainder when the number of ones in the length-$K$ window of $b$ with leftmost index $i$ is divided by two.

Output the minimum and maximum possible numbers of ones in Bessie's hidden binary string.

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

There are $T$ ($1\le T\le 10^3$) independent test cases to be solved. Each test is specified by the following:

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

The second line contains the binary string $r_1\dots r_{N-K+1}$, where $r_i=\sum_{j=i}^{j+K-1}b_j\pmod{2}$.

It is guaranteed that the sum of $N$ over all tests does not exceed $10^6$.

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

For each test case, output the minimum and maximum possible numbers of ones in Bessie's hidden binary string, separated by a single space.

SAMPLE INPUT:

7
5 1
10011
5 2
1001
5 3
100
5 5
0
5 5
1
4 4
1
5 2
0000

SAMPLE OUTPUT:

3 3
2 3
1 4
0 4
1 5
1 3
0 5

For the first test case, $K=1$ means that $r=b$, and the number of ones in $r$ is $3$.

For the second test case, there are two possibilities for $b$: 10001 and 01110, having $2$ and $3$ ones, respectively.

SCORING:

  • Input 2: $N\le 8$
  • Inputs 3-4: $K\le 8$ and the sum of $N$ over all tests does not exceed $10^4$.
  • Inputs 5-11: No additional constraints.

Problem credits: Benjamin Qi

结果

Kimi K3.1 ProKimi

auto(max)

1/1

解出的采样 ↑

  • 100采样 1 · 解出
  • 4.6k平均输出 token(含思考) ↓
  • 31.2输出速度 tokens/s ↑
  • 149 秒延迟 ↓
  • 0.92×CPU 时间 / 官方解 ↓
  • 同阶时间复杂度(相对官方解)
  • 4Pylint 问题 ↓
  • 8.95Pylint 得分 ↑
  • 4圈复杂度 ↓
  • 62.8可维护性指数 ↑

DeepSeek Flash V4.1-1017DeepSeek

auto(high)

1/1

解出的采样 ↑

  • 100采样 1 · 解出
  • 13.2k平均输出 token(含思考) ↓
  • 218.7输出速度 tokens/s ↑
  • 61 秒延迟 ↓
  • 0.59×CPU 时间 / 官方解 ↓
  • 不可测时间复杂度(相对官方解)
  • 10Pylint 问题 ↓
  • 8.39Pylint 得分 ↑
  • 14圈复杂度 ↓
  • 48.9可维护性指数 ↑

MiniMax M3.1 FlashMiniMax

auto(max)

1/1

解出的采样 ↑

  • 100采样 1 · 解出
  • 15.7k平均输出 token(含思考) ↓
  • 184.3输出速度 tokens/s ↑
  • 86 秒延迟 ↓
  • 0.88×CPU 时间 / 官方解 ↓
  • 同阶时间复杂度(相对官方解)
  • 4Pylint 问题 ↓
  • 8.92Pylint 得分 ↑
  • 9圈复杂度 ↓
  • 66.8可维护性指数 ↑

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

代码

Kimi K3.1 Pro · 采样 1 · 55 行
import sys


def solve():
    data = sys.stdin.buffer.read().split()
    t = int(data[0])
    idx = 1
    answers = []
    INF = 10**18

    for _ in range(t):
        n = int(data[idx])
        k = int(data[idx + 1])
        idx += 2
        r = data[idx]
        idx += 1

        required_parity = r[0] & 1
        base_ones = 0

        # DP over the parity of the number of initial bits set to 1.
        min_dp = [0, INF]
        max_dp = [0, -INF]

        for start in range(k):
            length = (n - 1 - start) // k + 1
            value = 0
            ones = 0

            # Values when this residue class's initial bit is zero.
            for i in range(start, n - k, k):
                value ^= r[i] ^ r[i + 1]
                ones += value

            base_ones += ones
            # Change in ones if the initial bit is flipped to one.
            delta = length - 2 * ones

            old_min0, old_min1 = min_dp
            old_max0, old_max1 = max_dp

            min_dp[0] = min(old_min0, old_min1 + delta)
            min_dp[1] = min(old_min1, old_min0 + delta)
            max_dp[0] = max(old_max0, old_max1 + delta)
            max_dp[1] = max(old_max1, old_max0 + delta)

        minimum = base_ones + min_dp[required_parity]
        maximum = base_ones + max_dp[required_parity]
        answers.append(f"{minimum} {maximum}")

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


if __name__ == "__main__":
    solve()