题面
来源: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
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
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
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 秒的题;复杂度按增长幂次相对官方解分类。判题与计分见 原理。
代码
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()
import sys def solve() -> None: data = sys.stdin.buffer.read().split() if not data: return t = int(data[0]) idx = 1 out = [] INF = 10**18 for _ in range(t): N = int(data[idx]) K = int(data[idx + 1]) idx += 2 r = data[idx] idx += 1 base = [0] * (N + 1) ones_by_edge = [0] * K prev = 0 for i in range(1, N + 1): if i >= K: cur = base[i - K] ^ (r[i - K] & 1) base[i] = cur else: cur = 0 if cur ^ prev: ones_by_edge[i % K] += 1 prev = cur min_sum = 0 max_sum = 0 min_parity = 0 max_parity = 0 min_penalty = INF max_penalty = INF for c in range(K): ones = ones_by_edge[c] if c == 0: total = N // K else: total = (N - c) // K + 1 zeros = total - ones # Minimum: choose the cheaper value of this edge variable. if ones <= zeros: min_sum += ones penalty = zeros - ones else: min_sum += zeros min_parity ^= 1 penalty = ones - zeros if penalty < min_penalty: min_penalty = penalty # Maximum: choose the more expensive value. if ones >= zeros: max_sum += ones penalty = ones - zeros else: max_sum += zeros max_parity ^= 1 penalty = zeros - ones if penalty < max_penalty: max_penalty = penalty if min_parity: min_sum += min_penalty if max_parity: max_sum -= max_penalty out.append(f"{min_sum} {max_sum}") sys.stdout.write("\n".join(out)) if __name__ == "__main__": solve()
import sys def solve_case(n, k, r): INF = 10**18 # DP state is the XOR of the starting bits of processed chains. min_even, min_odd = 0, INF max_even, max_odd = 0, -INF # An edge from position p to p + k exists exactly when p < n - k. edge_limit = n - k for start in range(k): pos = start relative = 0 cost0 = 0 cost1 = 0 while pos < n: # relative is this bit assuming the chain starts with 0. if relative: # Starting with 0 makes this bit 1. cost0 += 1 else: # Starting with 1 makes this bit 1. cost1 += 1 if pos < edge_limit and r[pos] != r[pos + 1]: relative ^= 1 pos += k # Choosing 0 preserves the parity; choosing 1 flips it. min_even, min_odd = ( min(min_even + cost0, min_odd + cost1), min(min_even + cost1, min_odd + cost0), ) max_even, max_odd = ( max(max_even + cost0, max_odd + cost1), max(max_even + cost1, max_odd + cost0), ) required = r[0] - ord("0") if required == 0: return min_even, max_even return min_odd, max_odd def main(): tokens = iter(sys.stdin.buffer.read().split()) t = int(next(tokens)) answers = [] for _ in range(t): n = int(next(tokens)) k = int(next(tokens)) r = next(tokens) minimum, maximum = solve_case(n, k, r) answers.append(f"{minimum} {maximum}") sys.stdout.write("\n".join(answers)) if __name__ == "__main__": main()