题目 › usaco-1519

Compatible Pairs

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

查看原题跳到结果

题面

来源: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:

118
A 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

auto(max)

1/1

解出的采样 ↑

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

DeepSeek Flash V4.1-1017DeepSeek

auto(high)

1/1

解出的采样 ↑

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

MiniMax M3.1 FlashMiniMax

auto(max)

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 秒的题;复杂度按增长幂次相对官方解分类。判题与计分见 原理。

代码

Kimi K3.1 Pro · 采样 1 · 98 行
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()