题目 › usaco-1446

Cake Game

USACO 2024 December · 银组 · 2024-12 · L2 中等 · Python 3.11 · 时限 4 秒 · 测试点等分

查看原题跳到结果

题面

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

Bessie and Elsie have discovered a row of $N$ cakes $(2 \leq N \leq 5\cdot 10^5, N \text{ even})$ cakes, with sizes $a_1,a_2,\dots,a_N$ in that order ($1\le a_i\le 10^9$).

Each cow wants to eat as much as possible. However, being very civilized cows, they decided to play a game to split it! The game proceeds with both cows alternating turns. Each turn consists of one of the following:

  1. Bessie chooses two adjacent cakes and stacks them, creating a new cake with the sum of the sizes.
  2. Elsie chooses either the leftmost or rightmost cake and puts it in her stash.

When only one cake remains, Bessie eats it, and Elsie eats all cakes in her stash. If both cows play optimally to maximize the amount of cake they eat and Bessie plays first, how much cake will each cow eat?

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

Each input consists of $T$ ($1\le T\le 10$) independent test cases. It is guaranteed that the sum of all $N$ within an input does not exceed $10^6$.

Each test case is formatted as follows. The first line contains $N$. The next line contains $N$ space-separated integers, $a_1,a_2,\ldots,a_N$.

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

For each test case, output a line containing $b$ and $e$, representing the amounts of cake Bessie and Elsie respectively will consume if both cows play optimally.

SAMPLE INPUT:

2
4
40 30 20 10
4
10 20 30 40

SAMPLE OUTPUT:

60 40
60 40

For the first test case, under optimal play,

  1. Bessie will stack the middle two cakes. The cakes now have sizes $[40,50,10]$.
  2. Elsie will eat the leftmost cake. The remaining cakes now have sizes $[50,10]$.
  3. Bessie stacks the remaining two cakes.

Bessie will eat $30+20+10=60$ cake, while Elsie will eat $40$ cake.

The second test case is the reverse of the first, so the answer is the same.

SCORING:

  • Input 2: All $a_i$ are equal.
  • Input 3: $N\le 10$
  • Inputs 4-7: $N\le 5000$
  • Inputs 8-11: No additional constraints.

Problem credits: Linda Zhao, Agastya Goel, Gavin Ye

结果

Kimi K3.1 ProKimi

auto(max)

1/1

解出的采样 ↑

  • 100采样 1 · 解出
  • 8.1k平均输出 token(含思考) ↓
  • 27.5输出速度 tokens/s ↑
  • 295 秒延迟 ↓
  • 0.63×CPU 时间 / 官方解 ↓
  • 同阶时间复杂度(相对官方解)
  • 3Pylint 问题 ↓
  • 8.57Pylint 得分 ↑
  • 5圈复杂度 ↓
  • 54.5可维护性指数 ↑

DeepSeek Flash V4.1-1017DeepSeek

auto(high)

1/1

解出的采样 ↑

  • 100采样 1 · 解出
  • 17.4k平均输出 token(含思考) ↓
  • 174.1输出速度 tokens/s ↑
  • 106 秒延迟 ↓
  • 0.61×CPU 时间 / 官方解 ↓
  • 同阶时间复杂度(相对官方解)
  • 3Pylint 问题 ↓
  • 8.70Pylint 得分 ↑
  • 5圈复杂度 ↓
  • 55.0可维护性指数 ↑

MiniMax M3.1 FlashMiniMax

auto(max)

1/1

解出的采样 ↑

  • 100采样 1 · 解出
  • 25.2k平均输出 token(含思考) ↓
  • 134.6输出速度 tokens/s ↑
  • 188 秒延迟 ↓
  • 0.50×CPU 时间 / 官方解 ↓
  • 同阶时间复杂度(相对官方解)
  • 3Pylint 问题 ↓
  • 8.85Pylint 得分 ↑
  • 5圈复杂度 ↓
  • 52.0可维护性指数 ↑

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

代码

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

def main():
    data = list(map(int, sys.stdin.buffer.read().split()))
    it = iter(data)
    t = next(it)
    answers = []

    for _ in range(t):
        n = next(it)
        a = [next(it) for _ in range(n)]

        total = sum(a)
        window_size = n // 2 + 1

        current = sum(a[:window_size])
        bessie = current

        for left in range(1, n - window_size + 1):
            current += a[left + window_size - 1] - a[left - 1]
            if current < bessie:
                bessie = current

        answers.append(f"{bessie} {total - bessie}")

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

if __name__ == "__main__":
    main()