题目 › usaco-1520

Ski Slope

USACO 2025 US Open · 银组 · 2025-03 · L2 中等 · Python 3.11 · 时限 4 秒 · 测试点等分

查看原题跳到结果

题面

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

Bessie is going on a ski trip with her friends. The mountain has $N$ waypoints ($1\leq N \leq 10^5$) labeled $1, 2, \ldots, N$ in increasing order of altitude (waypoint $1$ is the bottom of the mountain).

For each waypoint $i > 1$, there is a ski run starting from waypoint $i$ and ending at waypoint $p_i$ ($1\le p_i<i$). This run has difficulty $d_i$ ($0 \leq d_i \leq 10^9$) and enjoyment $e_i$ ($0 \leq e_i \leq 10^9$).

Each of Bessie's $M$ friends ($1\leq M \leq 10^5$) will do the following: They will pick some initial waypoint $i$ to start at, and then follow the runs downward (to $p_i$, then to $p_{p_i}$, and so forth) until they get to waypoint $1$.

The enjoyment each friend gets is equal to the sum of the enjoyments of the runs they follow. Each friend also has a different skill level $s_j$ ($0 \leq s_j \leq 10^9$) and courage level $c_j$ ($0 \leq c_j \leq 10$), which limits them to selecting an initial waypoint that results in them taking at most $c_j$ runs with difficulty greater than $s_j$.

For each friend, compute the maximum enjoyment they can get.

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

The first line contains $N$.

Then for each $i$ from $2$ to $N$, a line follows containing three space-separated integers $p_i$, $d_i$, and $e_i$.

The next line contains $M$.

The next $M$ lines each contain two space-separated integers $s_j$ and $c_j$.

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

Output $M$ lines, with the answer for each friend on a separate line.

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++).

SAMPLE INPUT:

4
1 20 200
2 30 300
2 10 100
8
19 0
19 1
19 2
20 0
20 1
20 2
29 0
30 0

SAMPLE OUTPUT:

0
300
500
300
500
500
300
500

  1. The first friend cannot start any waypoint other than $1$, since any other waypoint would cause them to take at least one run with difficulty greater than $19$. Their total enjoyment is $0$.
  2. The second friend can start at waypoint $4$ and take runs down to waypoint $2$ and then $1$. Their total enjoyment is $100+200=300$. They take one run with difficulty greater than $19$.
  3. The third friend can start at waypoint $3$ and take runs down to waypoint $2$ and then $1$. Their total enjoyment is $300+200=500$. They take two runs with difficulty greater than $19$.

SCORING:

  • Inputs 2-4: $N, M\le 1000$
  • Inputs 5-7: All $c_j=0$
  • Inputs 8-17: No additional constraints.

Problem credits: Brandon Wang

结果

Kimi K3.1 ProKimi

auto(max)

1/1

解出的采样 ↑

  • 100采样 1 · 解出
  • 13.0k平均输出 token(含思考) ↓
  • 23.9输出速度 tokens/s ↑
  • 549 秒延迟 ↓
  • —CPU 时间 / 官方解 ↓
  • 无官方解时间复杂度(相对官方解)
  • 7Pylint 问题 ↓
  • 9.01Pylint 得分 ↑
  • 19圈复杂度 ↓
  • 52.2可维护性指数 ↑

DeepSeek Flash V4.1-1017DeepSeek

auto(high)

1/1

解出的采样 ↑

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

MiniMax M3.1 FlashMiniMax

auto(max)

1/1

解出的采样 ↑

  • 100采样 1 · 解出
  • 10.8k平均输出 token(含思考) ↓
  • 138.1输出速度 tokens/s ↑
  • 79 秒延迟 ↓
  • —CPU 时间 / 官方解 ↓
  • 无官方解时间复杂度(相对官方解)
  • 5Pylint 问题 ↓
  • 9.11Pylint 得分 ↑
  • 15圈复杂度 ↓
  • 60.1可维护性指数 ↑

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

代码

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


def main():
    data = list(map(int, sys.stdin.buffer.read().split()))
    pos = 0

    n = data[pos]
    pos += 1

    parent = [0] * (n + 1)
    difficulty = [0] * (n + 1)
    enjoyment = [0] * (n + 1)

    for v in range(2, n + 1):
        p = data[pos]
        d = data[pos + 1]
        e = data[pos + 2]
        pos += 3
        parent[v] = p
        difficulty[v] = d
        enjoyment[v] = enjoyment[p] + e

    m = data[pos]
    pos += 1

    queries_by_courage = [[] for _ in range(11)]
    max_courage = 0

    for qid in range(m):
        s = data[pos]
        c = data[pos + 1]
        pos += 2
        queries_by_courage[c].append((s, qid))
        if c > max_courage:
            max_courage = c

    del data

    # tops[v] contains the largest max_courage+1 difficulties on the
    # path from waypoint 1 to v, in nonincreasing order.
    limit = max_courage + 1
    tops = [()] * (n + 1)

    for v in range(2, n + 1):
        a = tops[parent[v]]
        d = difficulty[v]

        if len(a) == limit and d <= a[-1]:
            tops[v] = a
            continue

        b = list(a)
        insert_at = 0
        while insert_at < len(a) and b[insert_at] >= d:
            insert_at += 1
        b.insert(insert_at, d)
        if len(b) > limit:
            b.pop()
        tops[v] = tuple(b)

    del parent, difficulty

    answers = [0] * m

    for c in range(max_courage + 1):
        queries = queries_by_courage[c]
        if not queries:
            continue

        candidates = []
        append_candidate = candidates.append

        # With courage c, waypoint v is usable exactly when its
        # (c+1)-st largest path difficulty is at most s. If the path
        # has at most c runs, every skill level is sufficient.
        for v in range(1, n + 1):
            tv = tops[v]
            required_skill = tv[c] if len(tv) > c else -1
            append_candidate((required_skill, enjoyment[v]))

        candidates.sort()
        queries.sort()

        candidate_index = 0
        best = 0

        for s, qid in queries:
            while (candidate_index < n and
                   candidates[candidate_index][0] <= s):
                value = candidates[candidate_index][1]
                if value > best:
                    best = value
                candidate_index += 1
            answers[qid] = best

    sys.stdout.write("\n".join(map(str, answers)) + "\n")


if __name__ == "__main__":
    main()