题目 › usaco-1541

Photoshoot

USACO 2026 First · 铜组 · 2026-01 · L1 基础 · Python 3.11 · 时限 4 秒 · 测试点等分

查看原题跳到结果

题面

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

Farmer John is looking at his cows in a magical field and wants to take pictures of subsets of his cows.

The field can be seen as a $N \times N$ grid $(1 \leq N \leq 500)$, with a single stationary cow at each location. Farmer John's camera is capable of taking a picture of any $K \times K$ square that is part of the field $(1 \leq K \leq \min(N, 25))$.

At all times, each cow has a beauty value between $0$ and $10^6$. The attractiveness index of a picture is the sum of the beauty values of the cows contained in the picture.

The beauty value for every cow starts out as $0$, so the attractiveness index of any picture in the beginning is $0$.

At $Q$ times $(1 \leq Q \leq 3\cdot 10^{4})$, the beauty of a single cow will increase by a positive integer due to eating the magical grass that is planted on Farmer John's field.

Farmer John wants to know the maximum attractiveness index of a picture he can take after each of the $Q$ updates.

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

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

The following line contains an integer $Q$.

Each of the following $Q$ lines contains three integers: $r$, $c$, and $v$, which are the row, column, and new beauty value, respectively ($1 \leq r, c \leq N, 1 \leq v \leq 10^6$). It is guaranteed that the new beauty value is greater than the beauty value at that location before.

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

Output $Q$ lines, corresponding to the maximum attractiveness index of a picture after each update.

SAMPLE INPUT:

4 2
3
2 2 11
3 4 3
3 1 100

SAMPLE OUTPUT:

11
11
111

After the first update, a picture with the maximum attractiveness index is the picture with upper left corner $(2, 2)$ and lower right corner $(3, 3)$, which has an attractiveness index of $11 + 0 + 0 + 0 = 11$.

The second update does not affect the maximum attractiveness index.

After the third update, the picture with the maximum attractiveness index changes to the picture with upper left corner $(2, 1)$ and lower right corner $(3, 2)$, which has an attractiveness index of $0 + 11 + 100 + 0 = 111$.

SAMPLE INPUT:

3 1
3
2 2 3
2 2 5
2 2 7

SAMPLE OUTPUT:

3
5
7

There is only one cow with a positive beauty value, so the maximum attractiveness index will always include that cow.

SCORING:

  • Inputs 3-6: $N \leq 50, Q \leq 100$
  • Inputs 7-10: $N \leq 50$
  • Inputs 11-18: No additional constraints.

Problem credits: Brian Law and Cici Liu

结果

Kimi K3.1 ProKimi

auto(max)

1/1

解出的采样 ↑

  • 100采样 1 · 解出
  • 1.9k平均输出 token(含思考) ↓
  • 28.5输出速度 tokens/s ↑
  • 69 秒延迟 ↓
  • —CPU 时间 / 官方解 ↓
  • 无官方解时间复杂度(相对官方解)
  • 4Pylint 问题 ↓
  • 8.86Pylint 得分 ↑
  • 8圈复杂度 ↓
  • 64.2可维护性指数 ↑

DeepSeek Flash V4.1-1017DeepSeek

auto(high)

0/1

解出的采样 ↑

  • 66.7采样 1 · 超时
  • 15.8k平均输出 token(含思考) ↓
  • 216.2输出速度 tokens/s ↑
  • 74 秒延迟 ↓
  • —CPU 时间 / 官方解 ↓
  • 无官方解时间复杂度(相对官方解)
  • 18Pylint 问题 ↓
  • 7.91Pylint 得分 ↑
  • 31圈复杂度 ↓
  • 42.7可维护性指数 ↑

MiniMax M3.1 FlashMiniMax

auto(max)

1/1

解出的采样 ↑

  • 100采样 1 · 解出
  • 6.5k平均输出 token(含思考) ↓
  • 152.6输出速度 tokens/s ↑
  • 43 秒延迟 ↓
  • —CPU 时间 / 官方解 ↓
  • 无官方解时间复杂度(相对官方解)
  • 4Pylint 问题 ↓
  • 8.75Pylint 得分 ↑
  • 9圈复杂度 ↓
  • 67.5可维护性指数 ↑

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

代码

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


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

    n, k = data[0], data[1]
    q = data[2]
    pos = 3

    beauty = [[0] * n for _ in range(n)]

    # sums[i][j] is the sum of the K x K square with upper-left corner (i, j).
    m = n - k + 1
    sums = [[0] * m for _ in range(m)]

    best = 0
    answers = []

    for _ in range(q):
        r = data[pos] - 1
        c = data[pos + 1] - 1
        value = data[pos + 2]
        pos += 3

        delta = value - beauty[r][c]
        beauty[r][c] = value

        # Only squares containing (r, c) change. Since every update is
        # positive, the global maximum can never decrease.
        i_start = max(0, r - k + 1)
        i_end = min(r, m - 1)
        j_start = max(0, c - k + 1)
        j_end = min(c, m - 1)

        for i in range(i_start, i_end + 1):
            row = sums[i]
            for j in range(j_start, j_end + 1):
                new_sum = row[j] + delta
                row[j] = new_sum
                if new_sum > best:
                    best = new_sum

        answers.append(str(best))

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


if __name__ == "__main__":
    main()