题目 › usaco-1448

2D Conveyor Belt

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

查看原题跳到结果

题面

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

Farmer John's milk factory can be described by an $N$ by $N$ ($1 \le N \le 1000$) grid of cells that contain conveyor belts. Position ($a,b$) describes the cell that is in the $a$-th row from the top and $b$-th column from the left. There are $5$ types of cells:

  1. "L" — the cell is a leftward facing conveyor belt which moves all items on it 1 cell left every time unit.
  2. "R" — the cell is a rightward facing conveyor belt which moves all items on it 1 cell right every time unit.
  3. "U" — the cell is an upward facing conveyor belt which moves all items on it 1 cell up every time unit.
  4. "D" — the cell is a downward facing conveyor belt which moves all items on it 1 cell down every time unit.
  5. "?" — Farmer John has not built a conveyor belt at that cell yet.

Note that conveyor belts can also move items outside the grid. A cell $c$ is unusable if an item placed at cell $c$ will never exit the conveyor belt grid (i.e. it will move around in the grid forever).

Initially, Farmer John has not started building the factory so all cells start out as "?". For the next $Q$ ($1 \le Q \le 2 \cdot 10^5$) days starting from day $1$ and ending at day $Q$, Farmer John will choose a cell that does not have a conveyor belt and build a conveyor belt at the cell.

Specifically, during the $i$-th day, Farmer John will build a conveyor belt of type $t_i$ ($t_i \in {\text{{L,R,U,D}}}$) at position ($r_i,c_i$) ($1 \le r_i,c_i \le N$). It is guaranteed that there is no conveyor belt at position ($r_i,c_i$).

After each day, help Farmer John find the minimum number of unusable cells he can achieve by optimally building conveyor belts on all remaining cells without a conveyor belt.

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

The first line contains $N$ and $Q$.

The $i$-th of the next $Q$ lines contains $r_i$, $c_i$, and $t_i$ in that order.

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

$Q$ lines, the $i$-th of which describing the minimum number of unusable cells if Farmer John fills optimally builds conveyor belts on all remaining cells that do not currently have a conveyor belt.

SAMPLE INPUT:

3 5
1 1 R
3 3 L
3 2 D
1 2 L
2 1 U

SAMPLE OUTPUT:

0
0
0
2
3
The conveyor belt after the fifth day is shown below.

RL?
U??
?DL

One optimal way to build conveyor belts on the remaining cells is as follows.

RLR
URR
LDL
In this configuration, the cells at ($1, 1$), ($1, 2$), and ($2, 1$) are unusable.

SAMPLE INPUT:

3 8
1 1 R
1 2 L
1 3 D
2 3 U
3 3 L
3 2 R
3 1 U
2 1 D

SAMPLE OUTPUT:

0
2
2
4
4
6
6
9
The conveyor belt after the eighth day is shown below.

RLD
D?U
URL
No matter what conveyor belt Farmer John can build at the center, all cells will be unusable.

SAMPLE INPUT:

4 13
2 2 R
2 3 R
2 4 D
3 4 D
4 4 L
4 3 L
4 2 U
3 1 D
4 1 R
2 1 L
1 1 D
1 4 L
1 3 D

SAMPLE OUTPUT:

0
0
0
0
0
0
0
0
11
11
11
11
13

SCORING:

  • Inputs 4-5: $N \le 10$
  • Inputs 6-7: $N \le 40$
  • Inputs 8-13: No additional constraints

Problem credits: Alex Liang

结果

Kimi K3.1 ProKimi

auto(max)

1/1

解出的采样 ↑

  • 100采样 1 · 解出
  • 30.6k平均输出 token(含思考) ↓
  • 25.6输出速度 tokens/s ↑
  • 1197 秒延迟 ↓
  • 1.53×CPU 时间 / 官方解 ↓
  • 同阶时间复杂度(相对官方解)
  • 6Pylint 问题 ↓
  • 9.70Pylint 得分 ↑
  • 38圈复杂度 ↓
  • 39.0可维护性指数 ↑

DeepSeek Flash V4.1-1017DeepSeek

auto(high)

1/1

解出的采样 ↑

  • 100采样 1 · 解出
  • 34.5k平均输出 token(含思考) ↓
  • 197.8输出速度 tokens/s ↑
  • 175 秒延迟 ↓
  • 1.00×CPU 时间 / 官方解 ↓
  • 同阶时间复杂度(相对官方解)
  • 20Pylint 问题 ↓
  • 8.73Pylint 得分 ↑
  • 44圈复杂度 ↓
  • 40.8可维护性指数 ↑

MiniMax M3.1 FlashMiniMax

auto(max)

1/1

解出的采样 ↑

  • 100采样 1 · 解出
  • 28.6k平均输出 token(含思考) ↓
  • 145.1输出速度 tokens/s ↑
  • 198 秒延迟 ↓
  • 0.32×CPU 时间 / 官方解 ↓
  • 同阶时间复杂度(相对官方解)
  • 13Pylint 问题 ↓
  • 8.82Pylint 得分 ↑
  • 39圈复杂度 ↓
  • 49.4可维护性指数 ↑

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

代码

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


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

    n = int(data[0])
    q = int(data[1])
    m = n * n

    # 0 = unknown, 1 = L, 2 = R, 3 = U, 4 = D
    direction = bytearray(m)
    positions = array("i", [0]) * q
    type_to_direction = {b"L": 1, b"R": 2, b"U": 3, b"D": 4}

    k = 2
    for i in range(q):
        r = int(data[k]) - 1
        c = int(data[k + 1]) - 1
        v = r * n + c
        positions[i] = v
        direction[v] = type_to_direction[data[k + 2]]
        k += 3
    del data

    # DSU for connected components of unknown cells.
    parent = list(range(m))
    size = [1] * m

    # Each component also maintains a linked list of its cells.
    head = array("i", range(m))
    tail = array("i", range(m))
    nxt = array("i", [-1]) * m

    component_good = bytearray(m)
    fixed_good = bytearray(m)

    def find(x):
        p = parent
        while p[x] != x:
            p[x] = p[p[x]]
            x = p[x]
        return x

    def union(a, b):
        a = find(a)
        b = find(b)
        if a == b:
            return a
        if size[a] < size[b]:
            a, b = b, a
        parent[b] = a
        size[a] += size[b]
        if component_good[b]:
            component_good[a] = 1
        nxt[tail[a]] = head[b]
        tail[a] = tail[b]
        return a

    # Build the components in the final configuration.
    for r in range(n):
        base = r * n
        for c in range(n):
            v = base + c
            if direction[v] == 0:
                if c + 1 < n and direction[v + 1] == 0:
                    union(v, v + 1)
                if r + 1 < n and direction[v + n] == 0:
                    union(v, v + n)

    bad_count = m
    stack = []
    last_row_start = m - n

    def mark_fixed_good(v):
        nonlocal bad_count
        if not fixed_good[v]:
            fixed_good[v] = 1
            bad_count -= 1
            stack.append(v)

    def activate_component(v):
        nonlocal bad_count
        root = find(v)
        if component_good[root]:
            return

        component_good[root] = 1
        bad_count -= size[root]

        # Every fixed belt pointing into this component now leads to an exit.
        cur = head[root]
        while cur != -1:
            col = cur % n

            if col:
                u = cur - 1
                if direction[u] == 2:       # R
                    mark_fixed_good(u)
            if col + 1 < n:
                u = cur + 1
                if direction[u] == 1:       # L
                    mark_fixed_good(u)
            if cur >= n:
                u = cur - n
                if direction[u] == 4:       # D
                    mark_fixed_good(u)
            if cur < last_row_start:
                u = cur + n
                if direction[u] == 3:       # U
                    mark_fixed_good(u)

            cur = nxt[cur]

    def drain_stack():
        while stack:
            v = stack.pop()
            col = v % n

            # Fixed predecessors become good. Adjacent unknown components
            # become good because they may choose to move into v.
            if col:
                u = v - 1
                d = direction[u]
                if d == 0:
                    activate_component(u)
                elif d == 2:
                    mark_fixed_good(u)

            if col + 1 < n:
                u = v + 1
                d = direction[u]
                if d == 0:
                    activate_component(u)
                elif d == 1:
                    mark_fixed_good(u)

            if v >= n:
                u = v - n
                d = direction[u]
                if d == 0:
                    activate_component(u)
                elif d == 4:
                    mark_fixed_good(u)

            if v < last_row_start:
                u = v + n
                d = direction[u]
                if d == 0:
                    activate_component(u)
                elif d == 3:
                    mark_fixed_good(u)

    # Initial sources of goodness: direct exits from the grid.
    for c in range(n):
        v = c
        if direction[v] == 0:
            activate_component(v)
        elif direction[v] == 3:             # U on top row
            mark_fixed_good(v)

        v = last_row_start + c
        if direction[v] == 0:
            activate_component(v)
        elif direction[v] == 4:             # D on bottom row
            mark_fixed_good(v)

    for r in range(n):
        v = r * n
        if direction[v] == 0:
            activate_component(v)
        elif direction[v] == 1:             # L on left column
            mark_fixed_good(v)

        v = r * n + n - 1
        if direction[v] == 0:
            activate_component(v)
        elif direction[v] == 2:             # R on right column
            mark_fixed_good(v)

    drain_stack()

    answers = [0] * (q + 1)
    answers[q] = bad_count

    # Remove belts in reverse order. Unknown components then only merge,
    # and the good set only grows.
    for day in range(q, 0, -1):
        v = positions[day - 1]
        old_good = fixed_good[v]

        # Temporarily regard v as a bad singleton unknown component.
        if old_good:
            bad_count += 1
            fixed_good[v] = 0

        direction[v] = 0
        parent[v] = v
        size[v] = 1
        head[v] = v
        tail[v] = v
        nxt[v] = -1
        component_good[v] = 0

        col = v % n
        neighbors = []
        if col:
            neighbors.append(v - 1)
        if col + 1 < n:
            neighbors.append(v + 1)
        if v >= n:
            neighbors.append(v - n)
        if v < last_row_start:
            neighbors.append(v + n)

        will_be_good = bool(old_good)
        if v < n or v >= last_row_start or col == 0 or col == n - 1:
            will_be_good = True

        roots = []
        for u in neighbors:
            if direction[u] == 0:
                root = find(u)
                if component_good[root]:
                    will_be_good = True
                if root not in roots:
                    roots.append(root)
            elif fixed_good[u]:
                will_be_good = True

        if will_be_good:
            activate_component(v)
            for root in roots:
                activate_component(root)
            drain_stack()

        current = v
        for root in roots:
            current = union(current, root)

        answers[day - 1] = bad_count

    sys.stdout.write("\n".join(map(str, answers[1:])))


solve()