题面
来源: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:
- "L" — the cell is a leftward facing conveyor belt which moves all items on it 1 cell left every time unit.
- "R" — the cell is a rightward facing conveyor belt which moves all items on it 1 cell right every time unit.
- "U" — the cell is an upward facing conveyor belt which moves all items on it 1 cell up every time unit.
- "D" — the cell is a downward facing conveyor belt which moves all items on it 1 cell down every time unit.
- "?" — 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 3The 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 LDLIn 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 9The conveyor belt after the eighth day is shown below.
RLD D?U URLNo 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
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
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
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 秒的题;复杂度按增长幂次相对官方解分类。判题与计分见 原理。
代码
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()
import sys from array import array def main(): data = sys.stdin.buffer.read().split() if not data: return it = iter(data) N = int(next(it)) Q = int(next(it)) M = N * N outside = M fixed_dir = bytearray(M) updates_u = [0] * Q updates_d = bytearray(Q) # Directions: 1 = L, 2 = R, 3 = U, 4 = D for i in range(Q): r = int(next(it)) - 1 c = int(next(it)) - 1 t = next(it)[0] idx = r * N + c if t == 76: # L d = 1 elif t == 82: # R d = 2 elif t == 85: # U d = 3 else: # D d = 4 updates_u[i] = idx updates_d[i] = d fixed_dir[idx] = d del data, it # Reverse graph representation: # head[v] is the first incoming edge u -> v stored in linked-list form. head = [-1] * (M + 1) to = array('i') nxt = array('i') ta = to.append na = nxt.append h = head ec = 0 Nn = N Mm = M fd = fixed_dir # Build the graph after all Q updates. for r in range(Nn): base = r * Nn up_ok = r > 0 down_ok = r + 1 < Nn idx = base for c in range(Nn): d = fd[idx] if d == 0: # Still '?': all four outgoing edges are possible. v = idx - 1 if c > 0 else Mm ta(idx); na(h[v]); h[v] = ec; ec += 1 v = idx + 1 if c + 1 < Nn else Mm ta(idx); na(h[v]); h[v] = ec; ec += 1 v = idx - Nn if up_ok else Mm ta(idx); na(h[v]); h[v] = ec; ec += 1 v = idx + Nn if down_ok else Mm ta(idx); na(h[v]); h[v] = ec; ec += 1 else: # Fixed conveyor belt: one outgoing edge. if d == 1: v = idx - 1 if c > 0 else Mm elif d == 2: v = idx + 1 if c + 1 < Nn else Mm elif d == 3: v = idx - Nn if up_ok else Mm else: v = idx + Nn if down_ok else Mm ta(idx); na(h[v]); h[v] = ec; ec += 1 idx += 1 # reach[v] means v can reach outside in the original graph. reach = bytearray(M + 1) reach[Mm] = 1 queue = [Mm] qpos = 0 reach_count = 0 # Initial reverse BFS from outside. while qpos < len(queue): v = queue[qpos] qpos += 1 e = h[v] while e != -1: u = to[e] if reach[u] == 0: reach[u] = 1 reach_count += 1 queue.append(u) e = nxt[e] ans = [0] * (Q + 1) ans[Q] = Mm - reach_count # Process updates backwards, adding removed edges. for i in range(Q - 1, -1, -1): u = updates_u[i] d = updates_d[i] r = u // Nn c = u - r * Nn if d != 1: v = u - 1 if c > 0 else Mm ta(u); na(h[v]); h[v] = ec; ec += 1 if reach[v] and not reach[u]: reach[u] = 1 reach_count += 1 queue.append(u) if d != 2: v = u + 1 if c + 1 < Nn else Mm ta(u); na(h[v]); h[v] = ec; ec += 1 if reach[v] and not reach[u]: reach[u] = 1 reach_count += 1 queue.append(u) if d != 3: v = u - Nn if r > 0 else Mm ta(u); na(h[v]); h[v] = ec; ec += 1 if reach[v] and not reach[u]: reach[u] = 1 reach_count += 1 queue.append(u) if d != 4: v = u + Nn if r + 1 < Nn else Mm ta(u); na(h[v]); h[v] = ec; ec += 1 if reach[v] and not reach[u]: reach[u] = 1 reach_count += 1 queue.append(u) while qpos < len(queue): v = queue[qpos] qpos += 1 e = h[v] while e != -1: u2 = to[e] if reach[u2] == 0: reach[u2] = 1 reach_count += 1 queue.append(u2) e = nxt[e] ans[i] = Mm - reach_count sys.stdout.write('\n'.join(str(ans[i]) for i in range(1, Q + 1))) if __name__ == "__main__": main()
import sys def flood(stack, good, belt, width): """Mark newly good predecessors of cells already in the stack.""" added = 0 push = stack.append pop = stack.pop # Directions: 0 = ?, 1 = L, 2 = R, 3 = U, 4 = D. while stack: target = pop() # Cell immediately to the left reaches target if it is ? or R. source = target - 1 if not good[source]: direction = belt[source] if direction == 0 or direction == 2: good[source] = 1 added += 1 push(source) # Cell immediately to the right reaches target if it is ? or L. source = target + 1 if not good[source]: direction = belt[source] if direction == 0 or direction == 1: good[source] = 1 added += 1 push(source) # Cell immediately above reaches target if it is ? or D. source = target - width if not good[source]: direction = belt[source] if direction == 0 or direction == 4: good[source] = 1 added += 1 push(source) # Cell immediately below reaches target if it is ? or U. source = target + width if not good[source]: direction = belt[source] if direction == 0 or direction == 3: good[source] = 1 added += 1 push(source) return added def solve(): read = sys.stdin.buffer.readline first = read().split() if not first: return n, q = map(int, first) # Use a one-cell padded border to represent the outside of the grid. width = n + 2 size = width * width # Border cells cannot be grid sources. # Directions: 0 = ?, 1 = L, 2 = R, 3 = U, 4 = D, 5 = border. belt = bytearray([5]) * size # The outside is reachable, while interior cells initially are not. good = bytearray([1]) * size zero_row = bytes(n) for row in range(1, n + 1): start = row * width + 1 belt[start:start + n] = zero_row good[start:start + n] = zero_row direction_code = { b"L": 1, b"R": 2, b"U": 3, b"D": 4, } positions = [0] * q # Construct the configuration after all q days. for i in range(q): row, col, direction = read().split() row = int(row) col = int(col) position = row * width + col positions[i] = position belt[position] = direction_code[direction] stack = [] usable = 0 # Seed cells that can move directly outside. for col in range(1, n + 1): # Top row: U or ?. position = width + col if not good[position]: direction = belt[position] if direction == 0 or direction == 3: good[position] = 1 usable += 1 stack.append(position) # Bottom row: D or ?. position = n * width + col if not good[position]: direction = belt[position] if direction == 0 or direction == 4: good[position] = 1 usable += 1 stack.append(position) for row in range(1, n + 1): # Left column: L or ?. position = row * width + 1 if not good[position]: direction = belt[position] if direction == 0 or direction == 1: good[position] = 1 usable += 1 stack.append(position) # Right column: R or ?. position = row * width + n if not good[position]: direction = belt[position] if direction == 0 or direction == 2: good[position] = 1 usable += 1 stack.append(position) # Propagate reachability backward through the grid. usable += flood(stack, good, belt, width) total_cells = n * n answers = [0] * q answers[-1] = total_cells - usable # Undo the days in reverse order. Replacing a fixed belt by ? # only adds possible moves, so the set of good cells only grows. for i in range(q - 1, -1, -1): position = positions[i] belt[position] = 0 if not good[position]: # For a boundary cell, one neighbor is the padded outside. if ( good[position - 1] or good[position + 1] or good[position - width] or good[position + width] ): good[position] = 1 usable += 1 stack.append(position) usable += flood(stack, good, belt, width) # After undoing day i+1, this is the configuration after day i. if i > 0: answers[i - 1] = total_cells - usable sys.stdout.write("\n".join(map(str, answers)) + "\n") if __name__ == "__main__": solve()