题面
来源:USACO 官网。模型收到的是同一份题面的文字。
Deep in the countryside, Farmer John’s cows aren’t just ordinary farm animals—they are part of a clandestine bovine intelligence network. Each cow carries an ID number, carefully assigned by the elite cow cryptographers. However, due to Farmer John's rather haphazard tagging system, some cows ended up with the same ID.
Farmer John noted that there are $N$ ($1\le N\le 2\cdot 10^5$) unique ID numbers, and for each unique ID $d_i$ ($0\le d_i\le 10^9$), there are $n_i$ ($1\le n_i\le 10^9$) cows who shared it.
The cows can only communicate in pairs, and their secret encryption method has one strict rule: two cows can only exchange information if they are not the same cow and the sum of their ID numbers equals either $A$ or $B$ ($0\le A\le B\le 2\cdot 10^9$). A cow can only engage in one conversation at a time (i.e., no cow can be part of more than one pair).
Farmer John would like to maximize the number of disjoint communication pairs to ensure the best information flow. Can you determine the largest number of conversations that can happen at once?
INPUT FORMAT (input arrives from the terminal / stdin):
The first line contains $N$, $A$, $B$.The next $N$ lines each contain $n_i$ and $d_i$. No two $d_i$ are the same.
OUTPUT FORMAT (print output to the terminal / stdout):
The maximum number of disjoint communicating pairs that can be formed at the same time.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 4 5 17 2 100 0 10 1 200 4
SAMPLE OUTPUT:
118A cow with an ID of $0$ can communicate with another cow with an ID of $4$ because the sum of their IDs is $4$. Since there are a total of $100$ cows of ID $0$ and $200$ cows of ID $4$, there can be up to $100$ communicating pairs with this combination of IDs.
A cow with an ID of $4$ can also communicate with another cow with an ID of $1$ (sum to $5$). There are $10$ cows of ID $1$ and $100$ remaining unpaired cows of ID $4$, allowing for another $10$ pairs.
Finally, a cow with an ID of $2$ can communicate with another cow of the same ID. Since there are a total of $17$ cows of ID $2$, there can be up to $8$ more pairs.
In total, there are $100+10+8=118$ communicating pairs. It can be shown that this is the maximum possible number of pairs.
SAMPLE INPUT:
4 4 5 100 0 10 1 100 3 20 4
SAMPLE OUTPUT:
30
Pairing IDs $0$ and $4$ makes $20$ pairs, while pairing IDs $1$ and $3$ makes $10$ pairs. It can be shown that this is the optimal pairing, resulting in a total of $30$ pairs.
SCORING:
- Inputs 3-4: $A=B$
- Inputs 5-7: $N\le 1000$
- Inputs 8-12: No additional constraints
Problem credits: Benjamin Qi
结果
Kimi K3.1 ProKimi
1/1
解出的采样 ↑
- 100采样 1 · 解出
- 29.0k平均输出 token(含思考) ↓
- 25.3输出速度 tokens/s ↑
- 1148 秒延迟 ↓
- —CPU 时间 / 官方解 ↓
- 无官方解时间复杂度(相对官方解)
- 7Pylint 问题 ↓
- 9.05Pylint 得分 ↑
- 28圈复杂度 ↓
- 48.6可维护性指数 ↑
DeepSeek Flash V4.1-1017DeepSeek
1/1
解出的采样 ↑
- 100采样 1 · 解出
- 58.8k平均输出 token(含思考) ↓
- 196.9输出速度 tokens/s ↑
- 299 秒延迟 ↓
- —CPU 时间 / 官方解 ↓
- 无官方解时间复杂度(相对官方解)
- 47Pylint 问题 ↓
- 8.48Pylint 得分 ↑
- 126圈复杂度 ↓
- 10.3可维护性指数 ↑
MiniMax M3.1 FlashMiniMax
1/1
解出的采样 ↑
- 100采样 1 · 解出
- 48.8k平均输出 token(含思考) ↓
- 169.0输出速度 tokens/s ↑
- 290 秒延迟 ↓
- —CPU 时间 / 官方解 ↓
- 无官方解时间复杂度(相对官方解)
- 9Pylint 问题 ↓
- 9.04Pylint 得分 ↑
- 30圈复杂度 ↓
- 55.9可维护性指数 ↑
↑ 越大越好,↓ 越小越好。得分 0–100,按官方时限判定,所有测试点都满分才算解出。CPU 时间 / 官方解为解出的答案在官方解最慢的 3 个测试点上的 CPU 时间之比(几何平均),只比较官方解至少需要 0.05 秒的题;复杂度按增长幂次相对官方解分类。判题与计分见 原理。
代码
import sys def solve(): data = list(map(int, sys.stdin.buffer.read().split())) if not data: return it = iter(data) n = next(it) A = next(it) B = next(it) counts = [0] * n ids = [0] * n for i in range(n): counts[i] = next(it) ids[i] = next(it) index = {d: i for i, d in enumerate(ids)} adj = [[] for _ in range(n)] self_pair = bytearray(n) sums = (A,) if A == B else (A, B) for i, d in enumerate(ids): for s in sums: other = s - d if other == d: self_pair[i] = 1 elif other > d: j = index.get(other) if j is not None: adj[i].append(j) adj[j].append(i) seen = bytearray(n) answer = 0 for root in range(n): if seen[root]: continue # Collect this component. The communication graph consists of paths. seen[root] = 1 stack = [root] component = [] loop_vertex = -1 while stack: v = stack.pop() component.append(v) if self_pair[v]: loop_vertex = v for w in adj[v]: if not seen[w]: seen[w] = 1 stack.append(w) if len(component) == 1: if self_pair[root]: answer += counts[root] // 2 continue endpoints = [v for v in component if len(adj[v]) == 1] # If a self-pairing ID exists, it is at one end. Greedily process # the path from the opposite end, then use cows left at that ID. if loop_vertex != -1: start = endpoints[0] if endpoints[0] != loop_vertex else endpoints[1] else: start = endpoints[0] previous = -1 v = start remaining = counts[v] while True: nxt = -1 for w in adj[v]: if w != previous: nxt = w break if nxt == -1: break pairs = min(remaining, counts[nxt]) answer += pairs remaining = counts[nxt] - pairs previous, v = v, nxt if self_pair[v]: answer += remaining // 2 sys.stdout.write(str(answer)) if __name__ == "__main__": solve()
import sys import math INF = 10**30 def get_order(comp, adj): if len(comp) == 1: return comp, False is_cycle = True for v in comp: if len(adj[v]) != 2: is_cycle = False break if is_cycle: start = comp[0] order = [] prev = -1 cur = start while True: order.append(cur) nxts = [u for u in adj[cur] if u != prev] if not nxts: break nxt = nxts[0] if nxt == start: break prev, cur = cur, nxt return order, True start = comp[0] for v in comp: if len(adj[v]) <= 1: start = v break order = [] prev = -1 cur = start while True: order.append(cur) nxts = [u for u in adj[cur] if u != prev] if not nxts: break nxt = nxts[0] prev, cur = cur, nxt return order, False def min_cover_path(weights): if not weights: return 0 dp0 = 0 dp1 = weights[0] for w in weights[1:]: ndp0 = dp1 ndp1 = min(dp0, dp1) + w dp0, dp1 = ndp0, ndp1 return min(dp0, dp1) def min_cover_cycle(weights): n = len(weights) if n == 0: return 0 if n == 1: return 0 ans = INF for first in (0, 1): dp0 = 0 if first == 0 else INF dp1 = weights[0] if first == 1 else INF for w in weights[1:]: ndp0 = dp1 ndp1 = min(dp0, dp1) + w dp0, dp1 = ndp0, ndp1 if first == 0: val = dp1 else: val = min(dp0, dp1) if val < ans: ans = val return ans def min_cover_forced(order, is_cycle, weights, forced_pos): n = len(order) if not is_cycle: f = forced_pos.get(0) dp0 = 0 if (f is None or f == 0) else INF dp1 = weights[0] if (f is None or f == 1) else INF for i in range(1, n): w = weights[i] ndp0 = dp1 ndp1 = min(dp0, dp1) + w f = forced_pos.get(i) if f == 0: ndp1 = INF elif f == 1: ndp0 = INF dp0, dp1 = ndp0, ndp1 return min(dp0, dp1) ans = INF f0 = forced_pos.get(0) for first in (0, 1): if f0 is not None and first != f0: continue dp0 = 0 if first == 0 else INF dp1 = weights[0] if first == 1 else INF for i in range(1, n): w = weights[i] ndp0 = dp1 ndp1 = min(dp0, dp1) + w f = forced_pos.get(i) if f == 0: ndp1 = INF elif f == 1: ndp0 = INF dp0, dp1 = ndp0, ndp1 if first == 0: val = dp1 else: val = min(dp0, dp1) if val < ans: ans = val return ans def solve_loop1(c, statuses): A0 = INF A1 = INF for A, s in statuses: if s == 0: A0 = min(A0, A) else: A1 = min(A1, A) def eval_e(e): ext = INF if A0 < INF: ext = min(ext, A0) if A1 < INF: ext = min(ext, A1 + e) if ext >= INF: return -INF return ext + (c - e) // 2 best = 0 candidates = set() for e in (0, 1, c - 1, c): if 0 <= e <= c: candidates.add(e) if A0 < INF and A1 < INF: cross = A0 - A1 for d in range(-10, 11): e = cross + d if 0 <= e <= c: candidates.add(e) for e in candidates: val = eval_e(e) if val > best: best = val return best def solve_loop2(c1, c2, statuses): best = 0 for p1 in (0, 1): for p2 in (0, 1): r1 = (c1 - p1) % 2 r2 = (c2 - p2) % 2 const = c1 + c2 - r1 - r2 forms = [] for A, s0, s1 in statuses: if A >= INF: continue A2 = 2 * A + const a = 2 * s0 - 1 b = 2 * s1 - 1 forms.append((A2, a, b)) if not forms: continue cand_points = [ (0, 0), (0, c2), (c1, 0), (c1, c2), ] m = len(forms) # Intersections of equality lines with rectangle boundaries. for i in range(m): Ai, ai, bi = forms[i] for j in range(i + 1, m): Aj, aj, bj = forms[j] da = ai - aj db = bi - bj C = Aj - Ai if da == 0 and db == 0: continue if db != 0: y = C / db if -1e-9 <= y <= c2 + 1e-9: cand_points.append((0, y)) y2 = (C - da * c1) / db if -1e-9 <= y2 <= c2 + 1e-9: cand_points.append((c1, y2)) if da != 0: x = C / da if -1e-9 <= x <= c1 + 1e-9: cand_points.append((x, 0)) x2 = (C - db * c2) / da if -1e-9 <= x2 <= c1 + 1e-9: cand_points.append((x2, c2)) # Triple intersections. for i in range(m): Ai, ai, bi = forms[i] for j in range(i + 1, m): Aj, aj, bj = forms[j] da1 = ai - aj db1 = bi - bj C1 = Aj - Ai for k in range(j + 1, m): Ak, ak, bk = forms[k] da2 = ai - ak db2 = bi - bk C2 = Ak - Ai det = da1 * db2 - da2 * db1 if abs(det) < 1e-12: continue x = (C1 * db2 - C2 * db1) / det y = (da1 * C2 - da2 * C1) / det if ( -1e-9 <= x <= c1 + 1e-9 and -1e-9 <= y <= c2 + 1e-9 ): cand_points.append((x, y)) # Check integer points with the required parities near all # continuous candidates. for xr, yr in cand_points: bx = math.floor(xr) by = math.floor(yr) for dx in range(-6, 7): x = bx + dx if x < 0 or x > c1 or (x & 1) != p1: continue for dy in range(-6, 7): y = by + dy if y < 0 or y > c2 or (y & 1) != p2: continue ext = INF for A, s0, s1 in statuses: if A >= INF: continue val = A + s0 * x + s1 * y if val < ext: ext = val if ext >= INF: continue total = ext + (c1 - x) // 2 + (c2 - y) // 2 if total > best: best = total return best def main(): data = sys.stdin.buffer.read().split() if not data: return it = iter(data) N = int(next(it)) A = int(next(it)) B = int(next(it)) ids = [] counts = [] index = {} for i in range(N): n = int(next(it)) d = int(next(it)) counts.append(n) ids.append(d) index[d] = i is_loop = [False] * N for i, d in enumerate(ids): if 2 * d == A or 2 * d == B: is_loop[i] = True adj = [[] for _ in range(N)] sums = (A,) if A == B else (A, B) for i, d in enumerate(ids): for S in sums: other = S - d if other == d: continue j = index.get(other) if j is not None and j > i: adj[i].append(j) adj[j].append(i) visited = [False] * N answer = 0 for start in range(N): if visited[start]: continue comp = [] stack = [start] visited[start] = True while stack: v = stack.pop() comp.append(v) for u in adj[v]: if not visited[u]: visited[u] = True stack.append(u) order, is_cycle = get_order(comp, adj) loops = [v for v in comp if is_loop[v]] if not loops: weights = [counts[v] for v in order] if is_cycle: answer += min_cover_cycle(weights) else: answer += min_cover_path(weights) else: order_pos = {v: i for i, v in enumerate(order)} weights_rest = [ 0 if is_loop[v] else counts[v] for v in order ] loops.sort(key=lambda v: order_pos[v]) L = len(loops) statuses = [] for mask in range(1 << L): forced_pos = {} s_list = [] for k, v in enumerate(loops): s = (mask >> k) & 1 s_list.append(s) forced_pos[order_pos[v]] = s cost = min_cover_forced( order, is_cycle, weights_rest, forced_pos ) if cost < INF: statuses.append((cost, s_list)) if L == 1: c = counts[loops[0]] stat1 = [(A_cost, slist[0]) for A_cost, slist in statuses] answer += solve_loop1(c, stat1) else: c1 = counts[loops[0]] c2 = counts[loops[1]] stat2 = [ (A_cost, slist[0], slist[1]) for A_cost, slist in statuses ] answer += solve_loop2(c1, c2, stat2) print(answer) if __name__ == "__main__": main()
import sys def solve(): data = list(map(int, sys.stdin.buffer.read().split())) if not data: return N, A, B = data[0], data[1], data[2] count = [0] * N ids = [0] * N index = {} p = 3 for i in range(N): count[i] = data[p] ids[i] = data[p + 1] index[ids[i]] = i p += 2 del data # Connect distinct ID groups whose IDs sum to A or B. adj = [[] for _ in range(N)] get_index = index.get for i, d in enumerate(ids): j = get_index(A - d, -1) if j != -1 and j != i and i < j: adj[i].append(j) adj[j].append(i) if A != B: j = get_index(B - d, -1) if j != -1 and j != i and i < j: adj[i].append(j) adj[j].append(i) del index, get_index # These IDs can form pairs with another cow having the same ID. self_compatible = bytearray(N) for i, d in enumerate(ids): if 2 * d == A or 2 * d == B: self_compatible[i] = 1 INF = 10**30 def cover_stats(nodes): """ For a weighted path: first result = minimum vertex-cover cost second result = minimum vertex-cover cost when its first node is required to be selected. By bipartite matching duality, these are also maximum matching sizes for the corresponding path. """ if not nodes: return 0, 0 w = count[nodes[0]] selected = w unselected = 0 forced_selected = w forced_unselected = INF for k in range(1, len(nodes)): w = count[nodes[k]] selected, unselected = ( w + min(selected, unselected), selected, ) forced_selected, forced_unselected = ( w + min(forced_selected, forced_unselected), forced_selected, ) return min(selected, unselected), min(forced_selected, forced_unselected) visited = bytearray(N) answer = 0 # Every component of the distinct-ID graph is a path. for start in range(N): if visited[start] or len(adj[start]) > 1: continue path = [] previous = -1 current = start while current != -1: visited[current] = 1 path.append(current) next_node = -1 for neighbor in adj[current]: if neighbor != previous: next_node = neighbor break previous, current = current, next_node if len(path) == 1: if self_compatible[path[0]]: answer += count[path[0]] // 2 continue has_special_endpoint = ( self_compatible[path[0]] or self_compatible[path[-1]] ) if not has_special_endpoint: # Ordinary path: greedily match adjacent groups from one end. remaining = count[path[0]] for k in range(1, len(path)): available = count[path[k]] take = remaining if remaining < available else available answer += take remaining = available - take continue # A self-compatible vertex can only be an endpoint of a path. # Put it first: special -- neighbor -- remaining path. if not self_compatible[path[0]]: path.reverse() special_count = count[path[0]] neighbor_count = count[path[1]] base, forced = cover_stats(path[2:]) gain = forced - base # If the neighbor has x cows, the maximum matching in the suffix # is base + min(x, gain). # # Choose t pairs between the special vertex and its neighbor. # The objective is: # t + floor((special_count - t) / 2) # + base + min(neighbor_count - t, gain). # # Its maximum occurs at neighbor_count - gain, clamped to the # feasible interval. max_external = min(special_count, neighbor_count) t = neighbor_count - gain if t < 0: t = 0 elif t > max_external: t = max_external suffix_matching = base + min(neighbor_count - t, gain) answer += ( t + (special_count - t) // 2 + suffix_matching ) print(answer) if __name__ == "__main__": solve()