题目 › usaco-1543

Mooclear Reactor

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

查看原题跳到结果

题面

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

Bessie is designing a nuclear reactor to power Farmer John's lucrative new AI data center business, CowWeave!

The reactor core consists of $N$ ($1\le N \le 2\cdot 10^5$) fuel rods, numbered $1$ through $N$. The $i$-th rod has a "stable operating range" $[l_i, r_i]$ ($-10^9 \leq l_i \leq r_i \leq 10^9$), meaning it can only generate power if its energy $a_i$ (chosen by Bessie) satisfies $l_i \le a_i \le r_i$; otherwise, it sits idle and does not generate power. Moreover, $a_i$ must always be an integer. Note that $a_i$ can be any integer, not limited to $[-10^9, 10^9]$.

However, quantum interactions between the rods mean that there are $M$ constraints of the form $(x, y, z)$ where Bessie must satisfy $a_x + a_y = z$ ($1 \leq x,y \leq N$ and $-10^9\le z\le 10^9$) to prevent the reactor from melting down.

Help Bessie find the maximum number of power-generating rods she can achieve in her design without it melting down!

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

The first line contains $T$ ($1\le T\le 10$), the number of independent tests. Each test is specified in the following format:

  • The first line contains the two integers $N$ and $M$.
  • The second line contains the $N$ integers $l_1, \dots, l_N$.
  • The third line contains the $N$ integers $r_1, \dots, r_N$.
  • The next $M$ lines each contain three integers $x$, $y$, and $z$, each representing a constraint.

It is guaranteed that neither the sum of $N$ nor the sum of $M$ over all tests exceeds $4\cdot 10^5$.

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

If no choice of rod energies exists that satisfies all constraints, output $-1$. Otherwise, output the maximum number of power-generating rods Bessie can achieve.

SAMPLE INPUT:

2
3 3
1 2 3
1 2 3
1 1 2
2 2 10
1 1 4
3 2
1 2 3
1 2 3
1 1 2
2 2 10

SAMPLE OUTPUT:

-1
2

In the second test, the constraints require that:

  1. $a_1 + a_1 = 2$
  2. $a_2 + a_2 = 10$

Choosing energies $a=[1, 5, 3]$ results in $2$ power-generating rods because:

  • $l_1 = 1 \leq a_1 \leq 1 = r_1$
  • $l_3 = 3 \leq a_3 \leq 3 = r_3$

and $a$ satisfies all required constraints.

SAMPLE INPUT:

1
3 2
10 -10 10
10 -10 10
1 2 0
2 3 0

SAMPLE OUTPUT:

3

Choosing rod energies $a=[10, -10, 10]$ results in $3$ power-generating rods.

SAMPLE INPUT:

5
3 3
1 -1 0
2 1 2
1 2 1
1 3 4
2 3 3
1 1
-100
100
1 1 3
1 1
-100
100
1 1 2
1 2
-100
100
1 1 2
1 1 4
1 2
-100
100
1 1 2
1 1 2

SAMPLE OUTPUT:

2
-1
1
-1
1

SCORING:

  • Input 4: $x = y$ for all constraints.
  • Inputs 5-7: $|x-y|=1$ for all constraints.
  • Inputs 8-10: $|x-y|\le 1$ for all constraints.
  • Inputs 11-13: No additional conditions.

Problem credits: Akshaj Arora

结果

Kimi K3.1 ProKimi

auto(max)

1/1

解出的采样 ↑

  • 100采样 1 · 解出
  • 10.3k平均输出 token(含思考) ↓
  • 28.3输出速度 tokens/s ↑
  • 368 秒延迟 ↓
  • —CPU 时间 / 官方解 ↓
  • 无官方解时间复杂度(相对官方解)
  • 11Pylint 问题 ↓
  • 9.19Pylint 得分 ↑
  • 38圈复杂度 ↓
  • 40.5可维护性指数 ↑

DeepSeek Flash V4.1-1017DeepSeek

auto(high)

0/1

解出的采样 ↑

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

MiniMax M3.1 FlashMiniMax

auto(max)

1/1

解出的采样 ↑

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

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

代码

Kimi K3.1 Pro · 采样 1 · 176 行
import sys
from array import array


def read_integers(data):
    i = 0
    n = len(data)
    while i < n:
        while i < n and data[i] <= 32:
            i += 1
        if i >= n:
            break
        sign = 1
        if data[i] == 45:
            sign = -1
            i += 1
        value = 0
        while i < n and data[i] > 32:
            value = value * 10 + data[i] - 48
            i += 1
        yield sign * value


def solve_test(n, left, right, head, to, nxt, edge_z):
    # sign: 0 = unvisited, 1 = a_i = b_i + t, 2 = a_i = b_i - t
    sign = bytearray(n)
    b = [0] * n
    answer = 0

    for root in range(n):
        if sign[root]:
            continue

        sign[root] = 1
        b[root] = 0
        component = [root]
        q = 0
        fixed_t = None

        while q < len(component):
            u = component[q]
            q += 1
            su = sign[u]
            su_value = 1 if su == 1 else -1
            bu = b[u]

            e = head[u]
            while e != -1:
                v = to[e]
                z = edge_z[e]
                sv = sign[v]

                if sv == 0:
                    sign[v] = 3 - su
                    b[v] = z - bu
                    component.append(v)
                elif sv != su:
                    # The parameter cancels, leaving a constant equation.
                    if bu + b[v] != z:
                        return -1
                else:
                    # 2 * su_value * t + bu + b[v] = z.
                    numerator = z - bu - b[v]
                    if numerator & 1:
                        return -1
                    t_value = (numerator // 2) * su_value
                    if fixed_t is None:
                        fixed_t = t_value
                    elif fixed_t != t_value:
                        return -1

                e = nxt[e]

        if fixed_t is not None:
            # This component has exactly one integer solution.
            for u in component:
                if sign[u] == 1:
                    value = b[u] + fixed_t
                else:
                    value = b[u] - fixed_t
                if left[u] <= value <= right[u]:
                    answer += 1
            continue

        if len(component) == 1:
            # Its operating range is nonempty and t is unrestricted.
            answer += 1
            continue

        # Each rod is active exactly when t belongs to one integer interval.
        # Maximize the number of intervals containing the chosen t.
        events = []
        for u in component:
            bu = b[u]
            if sign[u] == 1:
                lo = left[u] - bu
                hi = right[u] - bu
            else:
                lo = bu - right[u]
                hi = bu - left[u]
            events.append((lo, 1))
            events.append((hi + 1, -1))

        events.sort()
        current = 0
        best = 0
        i = 0
        total_events = len(events)

        while i < total_events:
            position = events[i][0]
            j = i
            # Apply all changes together because intervals are closed.
            while j < total_events and events[j][0] == position:
                current += events[j][1]
                j += 1
            if current > best:
                best = current
            i = j

        answer += best

    return answer


def main():
    data = sys.stdin.buffer.read()
    if not data:
        return

    integers = read_integers(data)
    tests = next(integers)
    output = []

    for _ in range(tests):
        n = next(integers)
        m = next(integers)

        left = array('i')
        right = array('i')
        for _ in range(n):
            left.append(next(integers))
        for _ in range(n):
            right.append(next(integers))

        head = array('i', [-1]) * n
        to = array('i')
        nxt = array('i')
        edge_z = array('i')

        for _ in range(m):
            x = next(integers) - 1
            y = next(integers) - 1
            z = next(integers)

            edge_id = len(to)
            to.append(y)
            edge_z.append(z)
            nxt.append(head[x])
            head[x] = edge_id

            if x != y:
                edge_id = len(to)
                to.append(x)
                edge_z.append(z)
                nxt.append(head[y])
                head[y] = edge_id

        result = solve_test(n, left, right, head, to, nxt, edge_z)
        output.append(str(result))

    sys.stdout.write('\n'.join(output))


if __name__ == '__main__':
    main()